mirror of https://github.com/E-Almqvist/hsf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
911 B
55 lines
911 B
# Repetition 2: Programmering 1
|
|
|
|
# 1
|
|
def max_of_two(num1, num2)
|
|
return num1 > num2 ? num1 : num2
|
|
end
|
|
|
|
# 2
|
|
def delete_char(str, char)
|
|
newstr = ""
|
|
str.split("").each do |c|
|
|
if( c == char ) then next end
|
|
newstr += c
|
|
end
|
|
|
|
return newstr
|
|
end
|
|
|
|
# 3
|
|
def distribution_calc(path)
|
|
distro_hash = Hash.new(0)
|
|
|
|
# Ladda in allt i filen till hashen
|
|
fh = File.open(path, "r")
|
|
items = fh.readlines()
|
|
total = items.length()
|
|
|
|
items.each do |item|
|
|
name, type = item.chomp().split(/\ #/)
|
|
distro_hash[type.to_sym()] += 1
|
|
end
|
|
fh.close()
|
|
|
|
# Printa ut datan
|
|
puts("Total: #{total}")
|
|
distro_hash.each do |type, amount| puts("#{type.to_s}: #{amount*100.0/total}%") end
|
|
|
|
return nil
|
|
end
|
|
|
|
# 4
|
|
def replace_substring(str, rep, sub, i)
|
|
newstr = str.dup()
|
|
|
|
rep_len = rep.length
|
|
if( newstr[i...i+rep_len] == rep ) then
|
|
newstr[i...i+rep_len] = sub
|
|
i += rep_len
|
|
end
|
|
|
|
|
|
rec_loop(0, newstr)
|
|
|
|
return newstr
|
|
end
|
|
|