06/17/15

# HackerRank ‘Staircase’ Solution

##### Short Problem Definition:

Your teacher has given you the task to draw the structure of a staircase. Being an expert programmer, you decided to make a program for the same. You are given the height of the staircase.

Staircase

##### Complexity:

time complexity is O(N^2)

space complexity is O(1)

##### Execution:

Either fill each level with N-i empty spaces or adjust to the right.

##### Solution:
```#!/usr/bin/py
def printStaircase(levels):
for i in xrange(1,levels+1):
print ("#" * i).rjust(levels)

if __name__ == '__main__':
t = input()
printStaircase(t)
```

[rust]
use std::io;

fn get_number() -> u32 {
let mut line = String::new();
io::stdin().read_line(&mut line).ok().expect("Failed to read line");
line.trim().parse::<u32>().unwrap()
}

fn main(){
let n = get_number() as usize;

for i in 0..n {
let s = std::iter::repeat("#").take(i+1).collect::<String>();
println!("{:>width\$}", s, width=n);
}

}
[/rust]

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

06/16/15

# HackerRank ‘Plus Minus’ Solution

##### Short Problem Definition:

You’re given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.

Plus Minus

##### Complexity:

time complexity is O(N)

space complexity is O(1)

##### Execution:

Count the values. Do not forget to force floats instead of integers.

##### Solution:
```#!/usr/bin/py

def getPartitionPythonesque(values):
c1 = len(filter(lambda x:x>0,values))
c2 = len(filter(lambda x:x<0,values))
c3 = len(filter(lambda x:x==0,values))
v_len = float(len(values))

return (c1/v_len, c2/v_len, c3/v_len)

def getPartition(values):
pos, neg, zero = [0.0,0.0,0.0]
v_len = len(values)

for value in values:
if value == 0:  zero += 1
elif value > 0: pos += 1
else:           neg += 1

return (pos/v_len, neg/v_len, zero/v_len)

if __name__ == '__main__':
t = input()
values = map(int, raw_input().split())
partition = getPartition(values)
for percentage in partition:
print round(percentage,4)
```

[rust]
use std::io;
use std::cmp::Ordering;

fn get_number() -> u32 {
let mut line = String::new();
io::stdin().read_line(&mut line).ok().expect("Failed to read line");
line.trim().parse::<u32>().unwrap()
}

fn get_numbers() -> Vec<i32> {
let mut line = String::new();
io::stdin().read_line(&mut line).ok().expect("Failed to read line");
line.split_whitespace().map(|s| s.parse::<i32>().unwrap()).collect()
}

fn main() {
let siz = get_number() as f32;

let (mut pos, mut neg, mut zer) = (0, 0, 0);

let zero = 0;

for i in get_numbers() {
match i.cmp(&zero) {
Ordering::Less => neg+=1,
Ordering::Greater => pos+=1,
Ordering::Equal => zer+=1,
}
}

println!("{:.6}\n{:.6}\n{:.6}", pos as f32 /siz, neg as f32 /siz, zer as f32 /siz);
}
[/rust]

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

06/15/15

# HackerRank ‘Diagonal Difference’ Solution

##### Short Problem Definition:

You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.

Given two strings (they can be of same or different length) help her in finding out the minimum number of character deletions required to make two strings anagrams. Any characters can be deleted from any of the strings.

##### Link

Diagonal Difference

##### Complexity:

time complexity is O(N)

space complexity is O(1)

##### Execution:

Access the correct indexes and compute the difference.

##### Solution:
```#!/usr/bin/py

def getDiagonalDifference(v):
diff = 0
v_len = len(v)
for idx in xrange(v_len):
diff += v[idx][idx]
diff -= v[idx][v_len-idx-1]

return abs(diff)

if __name__ == '__main__':
t = input()
v = []
for _ in xrange(t):
e = map(int, raw_input().split())
v.append(e)

print getDiagonalDifference(v)
```

[rust]
use std::io;

fn get_number() -> u32 {
let mut line = String::new();
io::stdin().read_line(&mut line).ok().expect("Failed to read line");
line.trim().parse::<u32>().unwrap()
}

fn get_numbers() -> Vec<i32> {
let mut line = String::new();
io::stdin().read_line(&mut line).ok().expect("Failed to read line");
line.split_whitespace().map(|s| s.parse::<i32>().unwrap()).collect()
}

fn main() {
let siz = get_number() as usize;

let mut left = 0;
let mut right = 0;

for i in 0..siz {
let item = get_numbers();
left += item[i];
right += item[siz-i-1];
}

println!("{}", (left-right).abs());

}
[/rust]

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.