This commit is contained in:
Filip Gralinski 2020-11-19 18:43:10 +01:00
commit f9900e247f
19 changed files with 770 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.pyc
Task*Test.xml
*~
pylint.log

29
blend.sh Executable file
View File

@ -0,0 +1,29 @@
#!/bin/zsh
. ./vars
cd ..
STUDENT_DIR=`ls -d ${PREFIX}-s??????`
echo "USING $STUDENT_DIR"
rm -rf arena
mkdir arena
cp -R $STUDENT_DIR/* arena/
ln -s ../$STUDENT_DIR/.git arena/.git
#mkdir -p arena/regexp
#mkdir -p arena/automata
rm -rf arena/odp arena/$PREFIX
rm arena/run.sh arena/vars
find arena -regex '.*/Task.*Test\.\(py\|dat\..*\)' -exec rm '{}' ';'
cp "${PREFIX}/run.sh" arena/run.sh
cp "${PREFIX}/vars" arena/vars
cp "${PREFIX}/overrides.txt" arena/overrides.txt
find ${PREFIX} -regex '.*/Task.*Test\.\(py\|dat\..*\)' -print | while read T; do cp $T ${T/${PREFIX}/arena}; done

12
intro/Task101.py Normal file
View File

@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
"""Rozwiązanie zadania 101."""
def even_elements(inlist):
"""Zwraca elementy listy o indeksach parzystych."""
return [inlist[i] for i in range(len(inlist)) if i % 2 == 0]
if __name__ == '__main__':
print(even_elements([1, 2, 3, 4]))

44
intro/Task101Test.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 101
Napisz funkcję `even_elements` zwracającą listę, która zawiera tylko
elementy z `list` o parzystych indeksach. (Zadanie kontrolne z gotowym
rozwiązaniem!)
NAME: even_elements
PARAMS: list
RETURN: list
POINTS: 1
"""
import unittest
from Task101 import even_elements
class Task101Test(unittest.TestCase):
"""Testy do zadania 101"""
def test_sequence(self):
"""Prosty test."""
self.assertEqual(even_elements([1, 2, 3, 4, 5, 6]), [1, 3, 5])
def test_empty(self):
"""Test na pustej liście."""
self.assertEqual(even_elements([]), [])
def test_singleton(self):
"""Test na liście jednoelementowej."""
self.assertEqual(even_elements([41]), [41])
def test_two_elements(self):
"""Test na liście dwuelementowej."""
self.assertEqual(even_elements([100, 2]), [100])
def test_negatives(self):
"""Test na liście z ujemnym elementem."""
self.assertEqual(even_elements([-31, 0, 2]), [-31, 2])
if __name__ == '__main__':
unittest.main()

30
intro/Task102Test.py Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 102
Napisz funkcję `add_three_numbers` zwracającą sumę trzech liczb całkowitych.
NAME: add_three_numbers
PARAMS: int, int, int
RETURN: int
POINTS: 3
"""
import unittest
from Task102 import add_three_numbers
class Task102Test(unittest.TestCase):
"""Testy do zadania 102"""
def test_simple(self):
"""Prosty test."""
self.assertEqual(add_three_numbers(2, 5, 7), 14)
def test_negatives(self):
"""Test z liczbami ujemnymi."""
self.assertEqual(add_three_numbers(-3, -1, 2), -2)
if __name__ == '__main__':
unittest.main()

46
intro/Task103Test.py Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 103
Napisz funkcję `probability` sprawdzającej, czy zadana liczba
reprezentuje prawdopodobieństwo (liczbę z zakresu od 0.0 do 1.0).
Jeśli tak, powinna zostać zwrócona ta liczba, w przeciwnym razie -
liczba 0.
NAME: probability
PARAMS: double
RETURN: double
POINTS: 2
"""
import unittest
from Task103 import probability
class Task103Test(unittest.TestCase):
"""Testy do zadania 103"""
def test_simple(self):
"""Prosty test."""
self.assertAlmostEqual(probability(0.62), 0.62)
def test_negatives(self):
"""Test z liczbami ujemnymi."""
self.assertAlmostEqual(probability(-0.6), 0.0)
self.assertAlmostEqual(probability(-1337), 0.0)
def test_special_cases(self):
"""Test z wartościami specjalnymi."""
self.assertEqual(probability(0.0), 0.0)
self.assertEqual(probability(1.0), 1.0)
def test_out_of_range(self):
"""Za duże wartości."""
self.assertEqual(probability(1.001), 0.0)
self.assertEqual(probability(972.12), 0.0)
if __name__ == '__main__':
unittest.main()

38
intro/Task104Test.py Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 104
Napisz funkcję `fahrenheit`, która przelicza stopnie Celsjusza na
stopnie Fahrenheita. Dla wartości mniejszych niż -273.15 funkcja
powinna zwracać zawsze -459.67.
NAME: fahrenheit
PARAMS: float
RETURN: float
POINTS: 2
"""
import unittest
from Task104 import fahrenheit
class Task104Test(unittest.TestCase):
"""Testy do zadania 104"""
def test_simple_cases(self):
"""Testy dla standardowych przypadków."""
self.assertAlmostEqual(fahrenheit(10), 50)
self.assertAlmostEqual(fahrenheit(33), 91.4)
self.assertAlmostEqual(fahrenheit(0), 32)
self.assertAlmostEqual(fahrenheit(-8), 17.6)
self.assertAlmostEqual(fahrenheit(-273), -459.4)
self.assertAlmostEqual(fahrenheit(-273.15), -459.67)
def test_below_zero(self):
"""Testy dla temperatur poniżej zera absolutnego."""
self.assertAlmostEqual(fahrenheit(-274), -459.67)
self.assertAlmostEqual(fahrenheit(-10000), -459.67)
if __name__ == '__main__':
unittest.main()

50
intro/Task105Test.py Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 105
Napisz funkcję `is_almost_prime(number, limit)` sprawdzającą, czy
podana liczba nie dzieli się przez żadną liczbę z zakresu od 2 do
`limit`. Dla liczb ujemnych powinna zostać zwrócona wartość `False`.
NAME: is_almost_prime
PARAMS: int, int
RETURN: bool
POINTS: 3
"""
import unittest
from Task105 import is_almost_prime
class Task105Test(unittest.TestCase):
"""Testy do zadania 105"""
def test_simple(self):
"""Proste testy"""
self.assertTrue(is_almost_prime(35, 4))
self.assertFalse(is_almost_prime(35, 5))
self.assertFalse(is_almost_prime(35, 100))
self.assertTrue(is_almost_prime(17, 4))
self.assertTrue(is_almost_prime(17, 16))
self.assertFalse(is_almost_prime(17, 17))
self.assertFalse(is_almost_prime(17, 100))
self.assertFalse(is_almost_prime(16, 2))
self.assertFalse(is_almost_prime(16, 5))
self.assertFalse(is_almost_prime(16, 13))
self.assertFalse(is_almost_prime(16, 101))
def test_special_cases(self):
"""Testy przypadków szczególnych."""
self.assertFalse(is_almost_prime(-1, 10))
self.assertFalse(is_almost_prime(0, 8))
self.assertTrue(is_almost_prime(1, 6))
self.assertFalse(is_almost_prime(2, 2))
def test_negatives(self):
"""Testy dla liczb ujemnych."""
self.assertFalse(is_almost_prime(-5, 100))
self.assertFalse(is_almost_prime(-13, 5))
if __name__ == '__main__':
unittest.main()

48
intro/Task106Test.py Executable file
View File

@ -0,0 +1,48 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 106
Napisz funkcję `penultimate(list, otherwise)` zwracającą przedostatni
element listy `list`. Jeśli lista jest pusta lub jednoelementowa,
należy zwrócić wartość `otherwise`.
NAME: penultimate
PARAMS: list, obj
RETURN: obj
POINTS: 2
"""
import unittest
from Task106 import penultimate
class Task106Test(unittest.TestCase):
"""Testy do zadania 106"""
def test_sequence(self):
"""Prosty test."""
self.assertEqual(
penultimate([1, 2, 3, 4, 5, 6], 'blabla'),
5)
def test_empty(self):
"""Test na pustej liście."""
self.assertEqual(
penultimate([], 'DO NOT WANT'),
'DO NOT WANT')
def test_singleton(self):
"""Test na liście jednoelementowej."""
self.assertEqual(
penultimate([41], 42),
42)
def test_two_elements(self):
"""Test na liście dwuelementowej."""
self.assertEqual(
penultimate(['x', 'y'], 'z'),
'x')
if __name__ == '__main__':
unittest.main()

40
intro/Task107Test.py Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 107
Napisz funkcję `list_cubed(list)` zwracającą sumę sześcianów
zadanych liczb. Dla listy pustej powinna zostać zwrócona wartość zero.
NAME: list_cubed
PARAMS: list
RETURN: obj
POINTS: 3
"""
import unittest
from Task107 import list_cubed
class Task107Test(unittest.TestCase):
"""Testy do zadania 107"""
def test_simple(self):
"""Prosty test."""
self.assertEqual(list_cubed([3, 0, -1, 2]), 34),
def test_empty(self):
"""Test na pustej liście."""
self.assertEqual(list_cubed([]), 0),
def test_singleton(self):
"""Test na liście jednoelementowej."""
self.assertEqual(list_cubed([-4]), -64)
def test_two_elements(self):
"""Test na liście dwuelementowej."""
self.assertEqual(list_cubed([100, -100]), 0)
self.assertEqual(list_cubed([2, 10]), 1008)
if __name__ == '__main__':
unittest.main()

37
intro/Task108Test.py Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 108
Napisz funkcję `pokemon_speak`, która zamienia w podanym napisie co
drugą literę na wielką. Np. `pokemon_speak('pokemon')` powinno zwrócić
'PoKeMoN'.
NAME: pokemon_speak
PARAMS: string
RETURN: string
POINTS: 4
"""
import unittest
from Task108 import pokemon_speak
class Task108Test(unittest.TestCase):
"""Testy do zadania 108."""
def test_special_cases(self):
"""Przypadki szczególne."""
self.assertEqual(pokemon_speak(''), '')
self.assertEqual(pokemon_speak('x'), 'X')
self.assertEqual(pokemon_speak('X'), 'X')
self.assertEqual(pokemon_speak('1337'), '1337')
def test_standard_cases(self):
"""Standardowe przypadki."""
self.assertEqual(pokemon_speak('pokemon'), 'PoKeMoN')
self.assertEqual(pokemon_speak('do not want'), 'Do nOt wAnT')
self.assertEqual(pokemon_speak('POKEMON'), 'POKEMON')
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,8 @@
raz
dwa
YES
cztery
YES
YES
YES
tego YES nie liczyć

31
intro/Task109Test.py Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 109
Napisz funkcję `count_yes_lines(filename)`, która po podaniu nazwy pliku
tekstowego `filename` zwraca liczbę wierszy w pliku zawierających
(wyłącznie) napis "YES" (i znak końca wiersza).
NAME: count_yes_lines
PARAMS: string
RETURN: int
POINTS: 3
"""
import unittest
from Task109 import count_yes_lines
class Task109Test(unittest.TestCase):
"""Testy do zadania 109."""
def test_on_self(self):
"""Testy na samym sobie."""
self.failUnlessEqual(count_yes_lines("Task109Test.py"), 0)
def test_simple(self):
"""Testy na prostym pliku."""
self.failUnlessEqual(count_yes_lines("Task109Test.dat.txt"), 4)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,6 @@
this is a string
Ala ma kota
blablabla
nic
pies, czyli kot

View File

@ -0,0 +1,6 @@
4 2 1 6
3 2 4
9
3
5 5 3

46
intro/Task110Test.py Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Zadanie 110
Napisz funkcję `words_to_length(infile, outfile)`, która wczytując
wiersze z pliku `infile` i wypisując je do pliku `outfile`, zamienia
wyrazy na liczbę odpowiadającą długości wyrazu. Np. wiersz `this is a
string` zostanie zamieniony na `4 2 1 6`. Zakładamy, że wyrazy
oddzielone spacjami (np. dla `pies, czyli kot` powinno wyjść `5 5
3`).
NAME: word_to_length
PARAMS: file, file
RETURN: string
POINTS: 5
"""
import unittest
from Task110 import word_to_length
class Task110Test(unittest.TestCase):
"""Testy do zadania 110"""
def test(self):
"""Prosty test przez porównanie z oczekiwanym wyjściem."""
infile = open("Task110Test.dat.in.txt", 'r')
out1 = open("Task110Test.out.txt", 'w')
word_to_length(infile, out1)
infile.close()
out1.close()
out1 = open("Task110Test.out.txt", 'r')
out2 = open("Task110Test.dat.out.txt", 'r')
for line1 in out1:
line2 = out2.readline()
self.assertEqual( line1, line2 )
out1.close()
out2.close()
if __name__ == '__main__':
unittest.main()

0
overrides.txt Normal file
View File

268
run.sh Executable file
View File

@ -0,0 +1,268 @@
#!/bin/zsh
. ./vars
PREFIX="$1"
OPTION="$2"
TASKS_DONE=0
POINTS_AWARDED=0
LOGFILE=`pwd`/log.all.txt
echo '' > "$LOGFILE"
typeset -A POINTS_PER_CLASSES
PYLINT_EXEC=pylint
run_pylint()
{
T=$1
DIR=$2
UNWANTED=$3
#pylint --disable=R0904,"$UNWANTED" $T ${T/%Test.py/.py} -f parseable | perl -pne "s{^Task}{${PREFIX}${DIR}/Task}" >> $PYLINT_LOG
$PYLINT_EXEC --disable=R0904,C0111,"$UNWANTED" ${T/%Test.py/.py} -f parseable | perl -pne "s{^Task}{${PREFIX}${DIR}/Task}" >> $PYLINT_LOG
PYLINT_EXIT_CODE=${pipestatus[1]}
}
run_pylint_single_file()
{
T=$1
DIR=$2
UNWANTED=$3
$PYLINT_EXEC --disable=R0904,C0111,"$UNWANTED" $T -f parseable | perl -pne "s{^}{${PREFIX}${DIR}/}" >> $PYLINT_LOG
PYLINT_EXIT_CODE=${pipestatus[1]}
}
process_test()
{
SOLUTION=${1/%Test.py/.py}
if [[ -r $SOLUTION ]]
then
echo "RUNNING $1"
JUST_NUMBER=${1/%Test.py/}
JUST_NUMBER=${JUST_NUMBER/#Task/}
TASK_KEY="${STUDENT_NUMBER}_${JUST_NUMBER}"
WAS_OVERRIDDEN=no
if [[ $OVERRIDDEN_POINTS[$TASK_KEY] != "" ]]
then
echo "from overridden.txt: $OVERRIDDEN_POINTS[$TASK_KEY]"
SUCCESS=yes
WAS_OVERRIDDEN=yes
MSS="READ FROM FILE overridden.txt "
else
XML_FILE=${1/%.py/.xml}
nosetests3 --with-xunit --xunit-file="$XML_FILE" $1
TESTS_OK=$?
# run_pylint $1 $2
# PYLINT_OK=$PYLINT_EXIT_CODE
SUCCESS=yes
if [[ "$TESTS_OK" == "0" ]]
then
MSS="TESTS OK"
else
MSS="TESTS FAILED"
SUCCESS=no
fi
# if [[ "$PYLINT_OK" == "0" ]]
# then
# MSS="$MSS PYLINT OK"
# else
# MSS="$MSS PYLINT FAILED"
# # SUCCESS=no
# fi
if [[ "$JUST_NUMBER" -gt 205 && "$JUST_NUMBER" -le 219 ]]
then
if [[ `expr $JUST_NUMBER % 15` != `expr $STUDENT_NUMBER % 15` ]]
then
MSS="$MSS ZLA RESZTA Z DZIELENIA!"
SUCCESS=no
fi
fi
if [[ "$JUST_NUMBER" -gt 305 && "$JUST_NUMBER" -le 329 ]]
then
if [[ `expr $JUST_NUMBER % 25` != `expr $STUDENT_NUMBER % 25` ]]
then
MSS="$MSS ZLA RESZTA Z DZIELENIA!"
SUCCESS=no
fi
fi
fi
CLASSES=$SOLUTION[-6]
echo "task belongs to classes $CLASSES"
CLASSES_TIME_LIMIT=$TIME_LIMITS[$CLASSES]
echo "classes time limit is $CLASSES_TIME_LIMIT"
HOME_TIME_LIMIT=$HOME_TIME_LIMITS[$CLASSES]
echo "home time limit is $HOME_TIME_LIMIT"
HOME_POINT_LIMIT=$HOME_POINT_LIMITS[$CLASSES]
echo "home point limit is $HOME_POINT_LIMIT"
LAST_COMMIT_DATE=`git log -1 --format=%cd --date=iso $SOLUTION`
echo "last commit to solution is $LAST_COMMIT_DATE"
AT_HOME="no"
TASK_POINTS_FOR_HOME=0
if [[ "$WAS_OVERRIDDEN" == "no" && "$CLASSES_TIME_LIMIT" < "$LAST_COMMIT_DATE" ]]
then
if [[ "$HOME_TIME_LIMIT" < "$LAST_COMMIT_DATE" ]]
then
MSS="$MSS TOO LATE"
SUCCESS="no"
echo "solved too late!"
else
MSS="$MSS DONE AT HOME"
if [[ "$SUCCESS" == "yes" ]]
then
TASK_POINTS_FOR_HOME=1
if [[ "$JUST_NUMBER" -gt 205 && "$JUST_NUMBER" -le 219 ]]
then
TASK_POINTS_FOR_HOME=6
fi
if [[ "$JUST_NUMBER" -gt 305 && "$JUST_NUMBER" -le 329 ]]
then
TASK_POINTS_FOR_HOME=4
fi
HPPC=$HOME_POINTS_PER_CLASSES[$CLASSES]
HPPC=`expr $HPPC + $TASK_POINTS_FOR_HOME`
HOME_POINT_LIMIT=$HOME_POINT_LIMITS[$CLASSES]
if [[ $HPPC -gt $HOME_POINT_LIMIT ]]
then
TASK_POINTS_FOR_HOME=`expr $HOME_POINT_LIMIT - $HOME_POINTS_PER_CLASSES[$CLASSES]`
HPPC=$HOME_POINT_LIMIT
MSS="$MSS HOME LIMIT EXCEEDED, AWARDING: $TASK_POINTS_FOR_HOME points"
fi
MSS="$MSS HOME POINTS: $TASK_POINTS_FOR_HOME"
HPOINTS_PER_CLASSES[$CLASSES]=$HPPC
fi
AT_HOME="yes"
echo "solved at home!"
fi
fi
if [[ "$SUCCESS" == "yes" ]]
then
TASKS_DONE=`expr $TASKS_DONE + 1`
TASK_POINTS=`cat $1 | perl -ne 'print "\$1\n" if /^POINTS:\s*(\d+)/'`
if [[ "$AT_HOME" == "yes" ]]
then
TASK_POINTS=$TASK_POINTS_FOR_HOME
fi
if [[ $OVERRIDDEN_POINTS[$TASK_KEY] != "" ]]
then
TASK_POINTS=$OVERRIDDEN_POINTS[$TASK_KEY]
fi
MSS="$MSS POINTS: $TASK_POINTS"
PPC=$POINTS_PER_CLASSES[$CLASSES]
PPC=`expr $PPC + $TASK_POINTS`
POINT_LIMIT=$DEFAULT_POINT_LIMIT
if [[ $POINT_LIMITS[$CLASSES] != "" ]]
then
POINT_LIMIT=$POINT_LIMITS[$CLASSES]
fi
if [[ $PPC -gt $POINT_LIMIT ]]
then
TASK_POINTS=`expr $POINT_LIMIT - $POINTS_PER_CLASSES[$CLASSES]`
PPC=$POINT_LIMIT
MSS="$MSS LIMIT EXCEEDED, AWARDING: $TASK_POINTS points"
fi
echo "$MSS $TASK_POINTS" >> "$LOGFILE"
POINTS_PER_CLASSES[$CLASSES]=$PPC
POINTS_AWARDED=`expr $POINTS_AWARDED + $TASK_POINTS`
fi
SUMMARY="${SUMMARY}\n$1 $MSS"
else
if [[ "$OPTION" == "fullcheck" ]]
then
# TODO fix duplication problem
# run_pylint $1 $2 "F0401,E0602,F0001"
fi
echo "SKIPPING $1"
SUMMARY="${SUMMARY}\n$1 NO SOLUTION"
fi
}
process_directory()
{
echo "=============================="
echo "GOING INTO $1"
pushd $1
for T in Task*Test.py
do
process_test $T $1
done
popd
}
read_overrides()
{
perl -pne 's/\s*\#.*//' < overrides.txt | while read INDEX_NUMBER TASK_CODE POINTS;
do
TASK_KEY="${INDEX_NUMBER}_${TASK_CODE}"
OVERRIDDEN_POINTS[$TASK_KEY]=$POINTS
done
}
STUDENT_NUMBER=`pwd | perl -ne 'print "$1" if /${PREFIX}-s(\d+)/'`
echo "STUDENT INDEX NUMBER IS: $STUDENT_NUMBER"
typeset -A OVERRIDDEN_POINTS
read_overrides
PYLINT_LOG=`pwd`/pylint.log
rm -f "$PYLINT_LOG"
for D in *(/)
do
process_directory $D
done
echo "==============================================="
echo "SUMMARY"
echo $SUMMARY
echo ""
echo "TASKS DONE: $TASKS_DONE"
echo "POINTS: $POINTS_AWARDED"
echo "points" > result.csv
echo $POINTS_AWARDED >> result.csv
perl -pne 's{^zaut-2019}{arena/}' -i pylint.log

27
vars Normal file
View File

@ -0,0 +1,27 @@
PREFIX=pjfz-2020
DEFAULT_POINT_LIMIT=15
POINT_LIMIT=15
# 1 - intro/
# 2 - regexp/
# 3 - automata/
typeset -A TIME_LIMITS
set -A TIME_LIMITS \
"1" "2020-10-27 10:00:00 +0100" \
"2" "2020-01-31 23:59:59 +0100" \
"3" "2020-01-25 13:00:00 +0100"
typeset -A HOME_TIME_LIMITS
set -A HOME_TIME_LIMITS \
"1" "2020-10-27 10:45:00 +0100" \
"2" "2020-01-31 23:59:59 +0100" \
"3" "2020-01-25 13:00:00 +0100"
typeset -A HOME_POINT_LIMITS
set -A HOME_POINT_LIMITS \
"1" "6" \
"2" "0" \
"3" "0"