You do not have permission to edit this page, for the following reason:
You must confirm your email address before editing pages. Please set and validate your email address through your user preferences.
Please READ the information contained in the tool-tips (click on a question mark).
Free text:
The goal of this contest is not to find the best coder, but to enjoy coding together and to motivate others to start coding. If you can write a "hello world" program in any language, you are welcome. There will be certainly someone to help you to solve the tasks. We will use the following example problem to make us familiar with the contest system. === Problem === Mr. Foo has to determine if his students have passed the year. As he has many students which have written many tests, he can't use his calculator, because it would take him too long. He knows that you are a skilled hacker, so he has asked you to make a program to automate his task. He has '''S''' students. Each student has written '''N''' tests. The test scores ranges from 0 to 60 (both inclusive). A student has passed his year if he has an average score of more or equal to 30. The average is not rounded. === Input === You are given an input file. The first line consists of 2 integers: the number of students S (1 <= S <= 1000) and the number of tests N (1 <= N <= 1000). S lines follow, which consists of N integers, representing the scores of a student. === Output === You have to create an output file which contains one line per student. Each containing 'PASSED' if he has passed his year, otherwise 'FAILED'. === Input example === 4 2<br/> 29 30<br/> 30 30<br/> 50 60<br/> 0 58 === Output example === FAILED<br/> PASSED<br/> PASSED<br/> FAILED === Solutions === <div style="clear:both;"> ==== bash ==== <source lang="bash"> kwisatz@arbre:~/coding/bash% bash sba.sh i FAILED PASSED PASSED FAILED kwisatz@arbre:~/coding/bash% wc -c sba.sh 128 sba.sh kwisatz@arbre:~/coding/bash% cat sba.sh tail -n+2 $1|while read i;do k=0;s=0;for j in $i;do((++k));((s=s+j));done;[[ $s -ge $((30*k)) ]]&&echo PASSED||echo FAILED;done </source></div> ---- ==== php ==== <source lang="bash"> jedi@test:~/work/coding$ php foo.php FAILED PASSED PASSED FAILED jedi@test:~/work/coding$ wc -c foo.php 116 foo.php jedi@test:~/work/coding$ cat foo.php </source><source lang="php"> <?$h=file('i',2);$i=1;while($h[$i]) echo(((array_sum($a=explode(' ',$h[$i++]))/count($a))>=30)?PASSED:FAILED)."\n"; </source> ---- ==== awk ==== <source lang="bash"> gunstick@manion:~$ awk -f 1.awk test.txt FAILED PASSED PASSED FAILED gunstick@manion:~$ wc -c 1.awk 57 1.awk gunstick@manion:~$ cat 1.awk </source><source lang="awk"> {for(s=i=0;i<NF;)s+=$++i;$0=s/i<30?"FAILED":"PASSED"}NR>1 </source> <!-- got you! 56 {for(s=i=0;i<NF;)s+=$++i;$0=s<30*i?"FAILED":"PASSED"}a++ --> ---- ==== perl ==== <source lang="bash"> gunstick@manion:~/contest$ perl foo.pl foo1.in FAILED PASSED PASSED FAILED gunsick@manion:~/contest$ wc -c foo.pl 75 foo.pl gunstick@manion:~/contest$ cat foo.pl </source><source lang="perl"> <>;while(<>){$s=0;$n=map{$s+=$_}split;print $s/$n<30?"FAILED\n":"PASSED\n"} </source> ---- ==== python ==== <source lang="python"> # CodingContest - Help Mister Foo # No focus on getting a short code rfile = open("marks.txt") wfile = open("results.txt",'w') students, tests = rfile.readline().split(" ") for students in rfile: values = students.rstrip("\n").split(" ") if (sum(int(x) for x in values)) / float(tests) < 30: wfile.write("FAILED!\n") else: wfile.write("PASSED!\n") rfile.close() wfile.close() </source> --- <source lang="bash"> Host:08-13-2011 user$ python3 Help_Mister_Foo.py Host:08-13-2011 user$ cat results.txt Failed! Passed! Passed! Failed! Host:08-13-2011 user$ wc -c Help_Mister_Foo.py 358 Help_Mister_Foo.py </source> ---- ==== python (alternate version) ==== <source lang="python"> f = open("marks.txt") f2 = open("results.txt",'w') students, tests = f.readline().split(" ") studentscounter = 0 while studentscounter < int(students): string = "Failed\n" if (sum([int(i) for i in f.readline().split(" ")])) / int(tests) < 30 else "Passed\n" f2.write(string) studentscounter = studentscounter + 1 f.close() f2.close() </source> --- <source lang="bash"> $ python2 marks.py $ cat results.txt Failed Passed Passed Failed $ wc -c marks.py 346 marks.py </source> ---- ==== python (alternate version 2) ==== <source lang="python"> f = open("marks.txt") f2 = open("results.txt",'w') students, tests = f.readline().split(" ") lines = f.read().split("\n")[0:-1] for values in lines: string = "Failed" if (sum([int(i) for i in values.split(" ")])) / int(tests) < 30 else "Passed" f2.write(string) f.close() f2.close() </source> --- <source lang="bash"> $ python2 marks.py $ cat results.txt Failed Passed Passed Failed $ wc -c marks.py 309 marks.py </source> ---- ==== lua ==== <source lang="bash"> kwisatz@arbre:~/coding/lua$ lua test.lua input.txt FAILED PASSED PASSED FAILED kwisatz@arbre:~/coding/lua$ wc -c test.lua 249 test.lua kwisatz@arbre:~/coding/lua$ cat test.lua </source> <source lang="lua"> io.input(arg[1]) local drop = io.read() for line in io.lines() do local t = 0 local n = 0 for mark in string.gmatch(line, "%d+") do t = t + mark n = n + 1 end if t >= 30 * n then print("PASSED") else print("FAILED") end end </source> ==== ruby ==== <source lang="bash"> x3nu@hoygrail ~/Desktop/syn2cat $ ruby foo.rb x3nu@hoygrail ~/Desktop/syn2cat $ cat results.txt Failed! Passed! Passed! Failed! x3nu@hoygrail ~/Desktop/syn2cat $ wc -c foo.rb 312 foo.rb x3nu@hoygrail ~/Desktop/syn2cat $ cat foo.rb </source> <source lang="ruby"> File.open("marks.txt", "r") do |marks| marks.gets while (line = marks.gets) File.open("results.txt", "a") do |results| a = line.split(' ').map(&:to_i) if a.inject {|sum,x| sum ? sum + x : x } / a.size >= 30 then results.write "Passed!\n" else results.write "Failed!\n" end end end end </source>
Summary:
This is a minor edit Watch this page
Cancel