Advertisement
Guest User

PAWO / Python3

a guest
Nov 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. Zadanie 0 - "Rozgrzewka"
  2.     a. Wyświetl swoje imię 100 razy.
  3.  
  4.     Solution:
  5.         print('John\n' * 100, end='')
  6.  
  7. Zadanie 1 - "100 lat"
  8.     a. Zapytaj użytkownika o imię oraz wiek.
  9.     b. Wyświetl informację, w którym roku użytkownik skończy 100 lat.
  10.  
  11.     Solution:
  12.         import datetime
  13.         year = datetime.datetime.now().year
  14.         name = input('Enter a name: ')
  15.         age  = int(input('Enter an age: '))
  16.         print('{} will be 100 years old in the year {}'.format(
  17.             name, year - age + 100
  18.         ))
  19.  
  20. Zadanie 2 - "Parzyste / Nieparzyste"
  21.     a. Zapytaj użytkownika o liczbę.
  22.     b. Wyświetl informację, czy liczba jest parzysta czy nieparzysta.
  23.  
  24.     Solution:
  25.         num = int(input('Enter a number: '))
  26.         print('Even' if num % 2 == 0 else 'Odd')
  27.  
  28. Zadanie 3 - "Mniejsze niż 10"
  29.     Dane:
  30.         data = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  31.  
  32.     a. Wyświetl elementy mniejsze niż 10.
  33.  
  34.     Solution:
  35.         print([x for x in data if x < 10])
  36.  
  37. Zadanie 4 - "Zgadula"
  38.     a. Wygeneruj losową liczbe od 1 do 9.
  39.     b. Zapytaj użytkownika o liczbę.
  40.     c. Wyświetl informację, czy liczba jest "mniejsza / większa / równa" wylosowanej.
  41.  
  42.     Solution:
  43.         import random
  44.         r_num = random.randint(1, 9)
  45.         num = int(input('Enter a number: '))
  46.         if num < r_num:
  47.             print('Random number was bigger!')
  48.         elif num > r_num:
  49.             print('Random number was smaller!')
  50.         else:
  51.             print('Perfect match!')
  52.  
  53. Zadanie 5 - "Podzielniki"
  54.     a. Zapytaj użytkownika o liczbę.
  55.     b. Wyświetl listę wszystkich podzielników danej liczby.
  56.  
  57.     Solution:
  58.         num = int(input('Enter a number: '))
  59.         print([x for x in range(1, num + 1) if num % x == 0])
  60.  
  61. Zadanie 6 - "Powtórzenia"
  62.     Dane:
  63.         a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  64.         b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  65.  
  66.     a. Wyświetl listę elementów występujących w obu listach (bez duplikatów).
  67.  
  68.     Solution:
  69.         print({x for x in a if x in b})
  70.  
  71. Zadanie 7 - "Napisy też mają indeksy!"
  72.     a. Zapytaj użytkownika o wyraz.
  73.     b. Wyświetl informację, czy wyraz jest palindromem
  74.         (palindrom -> czyli czytany wspak brzmi tak samo, np. "kajak")
  75.  
  76.     Solution:
  77.         word = input('Enter a word: ')
  78.         print('Palindrom' if word == word[::-1] else 'NOT Palindrom')
  79.  
  80. Zadanie 8 - "Fibonacci"
  81.     a. Napisz funkcję zwracającą N-pierwszych liczb Fibonacciego.
  82.         Wzór:
  83.             f_0 = 1, n == 0
  84.             f_1 = 1, n == 1
  85.             f_n = f_(n-2) + f_(n-1), n >= 2
  86.     b. Sprawdź, czy działa.
  87.  
  88.     Solution:
  89.         def fibonacci(n):
  90.             """ Yields generator of fibonacci numbers. """
  91.             if n >= 1:
  92.                 yield 1
  93.             if n >= 2:
  94.                 yield 1
  95.             a, b = 1, 1
  96.             for _ in range(2, n):
  97.                 c = a + b
  98.                 a, b = b, c
  99.                 yield c
  100.         for f in fibonacci(5):
  101.             print(f)
  102.  
  103. Zadanie 9 - "Licznik liter"
  104.     a. Napisz funkcję liczącą litery w danym napisie.
  105.  
  106.     Solution #1:
  107.         def count_letters(x):
  108.             """ Returns dictionary with each letter count. """
  109.             d = dict()
  110.             for letter in x:
  111.                 if letter not in d:
  112.                     d[letter] = 1
  113.                 else:
  114.                     d[letter] += 1
  115.             return d
  116.         print(count_letters('Hello World!'))
  117.  
  118.     Solution #2:
  119.         def count_letters(x):
  120.             """ Returns dictionary with each letter count. """
  121.             d = dict()
  122.             for letter in x:
  123.                 d.setdefault(letter, 0) += 1
  124.             return d
  125.         print(count_letters('Hello World!'))
  126.  
  127.  
  128. Zadanie 10 - "tr"
  129.     a. Napisz funkcję zwracającą zawartość podanego pliku.
  130.     b. Podmień zestaw znaków z sys.argv[1] na sys.argv[2] w zawartości pliku sys.argv[3].
  131.     c. Wyświetl podmienioną zawartość pliku na ekran.
  132.  
  133.     Solution:
  134.         import sys
  135.         def read_file(path):
  136.             """ Returns whole file content. """
  137.             with open(path) as f:
  138.                 return f.read()
  139.         data = read_file(path=sys.argv[3])
  140.         char_set_old = sys.argv[1]
  141.         char_set_new = sys.argv[2]
  142.         # That's ugly.
  143.         assert len(char_set_old) == len(char_set_new)
  144.         for i in range(len(char_set_old)):
  145.             data.replace(char_set_old[i], char_set_new[i])
  146.         print(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement