Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zadanie 0 - "Rozgrzewka"
- a. Wyświetl swoje imię 100 razy.
- Solution:
- print('John\n' * 100, end='')
- Zadanie 1 - "100 lat"
- a. Zapytaj użytkownika o imię oraz wiek.
- b. Wyświetl informację, w którym roku użytkownik skończy 100 lat.
- Solution:
- import datetime
- year = datetime.datetime.now().year
- name = input('Enter a name: ')
- age = int(input('Enter an age: '))
- print('{} will be 100 years old in the year {}'.format(
- name, year - age + 100
- ))
- Zadanie 2 - "Parzyste / Nieparzyste"
- a. Zapytaj użytkownika o liczbę.
- b. Wyświetl informację, czy liczba jest parzysta czy nieparzysta.
- Solution:
- num = int(input('Enter a number: '))
- print('Even' if num % 2 == 0 else 'Odd')
- Zadanie 3 - "Mniejsze niż 10"
- Dane:
- data = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- a. Wyświetl elementy mniejsze niż 10.
- Solution:
- print([x for x in data if x < 10])
- Zadanie 4 - "Zgadula"
- a. Wygeneruj losową liczbe od 1 do 9.
- b. Zapytaj użytkownika o liczbę.
- c. Wyświetl informację, czy liczba jest "mniejsza / większa / równa" wylosowanej.
- Solution:
- import random
- r_num = random.randint(1, 9)
- num = int(input('Enter a number: '))
- if num < r_num:
- print('Random number was bigger!')
- elif num > r_num:
- print('Random number was smaller!')
- else:
- print('Perfect match!')
- Zadanie 5 - "Podzielniki"
- a. Zapytaj użytkownika o liczbę.
- b. Wyświetl listę wszystkich podzielników danej liczby.
- Solution:
- num = int(input('Enter a number: '))
- print([x for x in range(1, num + 1) if num % x == 0])
- Zadanie 6 - "Powtórzenia"
- Dane:
- a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- a. Wyświetl listę elementów występujących w obu listach (bez duplikatów).
- Solution:
- print({x for x in a if x in b})
- Zadanie 7 - "Napisy też mają indeksy!"
- a. Zapytaj użytkownika o wyraz.
- b. Wyświetl informację, czy wyraz jest palindromem
- (palindrom -> czyli czytany wspak brzmi tak samo, np. "kajak")
- Solution:
- word = input('Enter a word: ')
- print('Palindrom' if word == word[::-1] else 'NOT Palindrom')
- Zadanie 8 - "Fibonacci"
- a. Napisz funkcję zwracającą N-pierwszych liczb Fibonacciego.
- Wzór:
- f_0 = 1, n == 0
- f_1 = 1, n == 1
- f_n = f_(n-2) + f_(n-1), n >= 2
- b. Sprawdź, czy działa.
- Solution:
- def fibonacci(n):
- """ Yields generator of fibonacci numbers. """
- if n >= 1:
- yield 1
- if n >= 2:
- yield 1
- a, b = 1, 1
- for _ in range(2, n):
- c = a + b
- a, b = b, c
- yield c
- for f in fibonacci(5):
- print(f)
- Zadanie 9 - "Licznik liter"
- a. Napisz funkcję liczącą litery w danym napisie.
- Solution #1:
- def count_letters(x):
- """ Returns dictionary with each letter count. """
- d = dict()
- for letter in x:
- if letter not in d:
- d[letter] = 1
- else:
- d[letter] += 1
- return d
- print(count_letters('Hello World!'))
- Solution #2:
- def count_letters(x):
- """ Returns dictionary with each letter count. """
- d = dict()
- for letter in x:
- d.setdefault(letter, 0) += 1
- return d
- print(count_letters('Hello World!'))
- Zadanie 10 - "tr"
- a. Napisz funkcję zwracającą zawartość podanego pliku.
- b. Podmień zestaw znaków z sys.argv[1] na sys.argv[2] w zawartości pliku sys.argv[3].
- c. Wyświetl podmienioną zawartość pliku na ekran.
- Solution:
- import sys
- def read_file(path):
- """ Returns whole file content. """
- with open(path) as f:
- return f.read()
- data = read_file(path=sys.argv[3])
- char_set_old = sys.argv[1]
- char_set_new = sys.argv[2]
- # That's ugly.
- assert len(char_set_old) == len(char_set_new)
- for i in range(len(char_set_old)):
- data.replace(char_set_old[i], char_set_new[i])
- print(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement