2020년 11월 17일 화요일

[Ruby] String Example

1. Time Addition

Ruby

hhmm = [ ]
"12:34 14:56 09:48".split.each { 
	|i| hhmm.append( i.split(":").map{ |i| i.to_i } ) 
}
hhmm = hhmm.transpose.map{ |i| i.reduce{ |a,b| a=a+b } }
print hhmm[0] + hhmm[1]/60, ":", hhmm[1]%60

Python
hhmm = [ 0, 0 ]
for i in "12:34 14:56 09:48".split():
    hm = i.split(":")
    hhmm[0] += int(hm[0]) 
    hhmm[1] += int(hm[1]) 
print (hhmm[0] + hhmm[1]//60, ":", hhmm[1]%60)

Julia
hhmm = [ 0, 0 ]
for i in split("12:34 14:56 09:48") 
    hm = split(i,":")
    hhmm[1] += parse(Int,hm[1]) 
    hhmm[2] += parse(Int,hm[2]) 
end
print( hhmm[1] + hhmm[2] ÷ 60, ":", hhmm[2]%60)


Output
37:18


댓글 없음:

댓글 쓰기