Z1-Rešenja

Table of Contents

  1. Primer 1
  2. Zadatak 1
  3. Zadatak 2
  4. Zadatak 3
  5. Zadatak 4
  6. Zadatak 5
  7. Zadatak 6
  8. Zadatak 7

Primer 1

import sys

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Error")
        sys.exit()
    key = sys.argv[1]
    val = [(i, i + 1) for i in range(0, 10, 2)]
    d = {}
    d[key] = val
    print(d)

primer1.py

Zadatak 1

def zbir(n):
    if n <= 0:
        return 0
    else:
        return n + zbir(n - 1)

if __name__ == "__main__":
    print(zbir(eval(input())))

zadatak1.py

Zadatak 2

import sys

def kv_zbir(n):
    if n <= 0:
        return 0
    else:
        return n**2 + kv_zbir(n - 1)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Error")
        sys.exit()
    n = int(sys.argv[1])
    print(kv_zbir(n))

zadatak2.py

Zadatak 3

if __name__ == "__main__":
    str1 = input()
    str2 = input()
    str3 = str1[:3] * 2 + str2[-3:];
    print(str3)

zadatak3.py

Zadatak 4

if __name__ == "__main__":
    l = [i + 1 for i in range(100)]
    print (l[::-1])

zadatak4.py

Zadatak 5

if __name__ == "__main__":
    f = open("dict_test.txt", "r")
    d = dict()
    for line in f:
        words = line.split()
        for word in words:
            if not word in d:
                d[word] = 1
            else:
                d[word] += 1
    for key, value in d.items():
        print(key, ":", value)
    f.close()

zadatak5.py

Zadatak 6

if __name__ == "__main__":
    l = []
    l.append((1, 2.3, "a"))
    l.append((4, 5.6, "abc"))
    l.append((7, 8.9, "ABC"))
    l.append((0, 1.2, "string"))
    print(l)
    l.remove(l[0])
    print(l)

zadatak6.py

Zadatak 7

if __name__ == "__main__":
    s = set()
    el0 = 1, 2.3, "a"
    el1 = 4, 5.6, "abc"
    el2 = 7, 8.9, "ABC"
    el3 = 0, 1.2, "string"
    s.add(el0)
    s.add(el1)
    s.add(el2)
    s.add(el3)
    print(s)
    s.remove(el0)
    print(s)

zadatak7.py