podaci: student.txt
ciljni rezultat:
originalni kod (Programming with The Canvas HTML5 CSS3 JavaScript Python Ruby Perl by Quan Nguyen):
i rezultat:
Gde je greska?
Moje rešenje:
i rezultat:
Radi, samo sto ima višak:
Kako ga eliminisati?
Code:
Dan 78 83 94 95
Tod 98 94 97 92
Leo 60 66 79 56
Dan 78 83 94 95
Tod 98 94 97 92
Leo 60 66 79 56
ciljni rezultat:
Code:
Leo 60 66 79 56 => 261
Dan 78 83 94 95 => 350
Tod 98 94 97 92 => 381
Leo 60 66 79 56 => 261
Dan 78 83 94 95 => 350
Tod 98 94 97 92 => 381
originalni kod (Programming with The Canvas HTML5 CSS3 JavaScript Python Ruby Perl by Quan Nguyen):
Code:
ruby -e'
tot = 0
print readlines.
collect{|line|
col=line.split();
col[1..-1].inject(0){|tot,x|tot+=x.to_i};
[tot,printf("%s => %d \n", line.chop, tot)]}.
sort{|a,b|b[0].to_i <=> a[0].to_i}.
collect {|y|y[1]}
' student.txt
ruby -e'
tot = 0
print readlines.
collect{|line|
col=line.split();
col[1..-1].inject(0){|tot,x|tot+=x.to_i};
[tot,printf("%s => %d \n", line.chop, tot)]}.
sort{|a,b|b[0].to_i <=> a[0].to_i}.
collect {|y|y[1]}
' student.txt
i rezultat:
Code:
Dan 78 83 94 95 => 0
Tod 98 94 97 92 => 0
Leo 60 66 79 56 => 0
[nil, nil, nil]
Dan 78 83 94 95 => 0
Tod 98 94 97 92 => 0
Leo 60 66 79 56 => 0
[nil, nil, nil]
Gde je greska?
Moje rešenje:
Code:
#!/usr/bin/ruby
tot = 0
File.open("student.txt","r") do |fh|
print fh.readlines.collect{|line|
col = line.split();
tot = col[1..-1].inject(0){|tot,x|tot+=x.to_i};
line = line.chop;
line += " => " + tot.to_s;}.
sort_by { |x| x[/\s+\d+$/].to_i }.
collect{|y|print "#{y} \n"}
end
print "\n"
#!/usr/bin/ruby
tot = 0
File.open("student.txt","r") do |fh|
print fh.readlines.collect{|line|
col = line.split();
tot = col[1..-1].inject(0){|tot,x|tot+=x.to_i};
line = line.chop;
line += " => " + tot.to_s;}.
sort_by { |x| x[/\s+\d+$/].to_i }.
collect{|y|print "#{y} \n"}
end
print "\n"
i rezultat:
Code:
Leo 60 66 79 56 => 261
Dan 78 83 94 95 => 350
Tod 98 94 97 92 => 381
[nil, nil, nil]
Leo 60 66 79 56 => 261
Dan 78 83 94 95 => 350
Tod 98 94 97 92 => 381
[nil, nil, nil]
Radi, samo sto ima višak:
Code:
[nil, nil, nil]
[nil, nil, nil]
Kako ga eliminisati?
Potpis
ER