SHOW:
|
|
- or go back to the newest paste.
| 1 | import random | |
| 2 | my_list = [] | |
| 3 | ||
| 4 | for i in range(20): | |
| 5 | value = random.randint(1, 100) | |
| 6 | my_list.append(value) | |
| 7 | ||
| 8 | # [10, 5, 8, 3, 15, 12, 17, 4, 9, 6, 19, 2, 7, 14, 16, 18, 20, 13, 11, 1] | |
| 9 | ||
| 10 | #======================================================== | |
| 11 | ||
| 12 | def bubble_sort(arr): | |
| 13 | n = len(arr) | |
| 14 | # iterujemy przez wszystkie elementy listy | |
| 15 | for i in range(n): | |
| 16 | # ostatnie i elementów są już posortowane | |
| 17 | for j in range(0, n-i-1): | |
| 18 | # porównujemy sąsiednie elementy | |
| 19 | if arr[j] > arr[j+1]: | |
| 20 | # zamieniamy miejscami, jeśli kolejność jest nieprawidłowa | |
| 21 | arr[j], arr[j+1] = arr[j+1], arr[j] | |
| 22 | return arr | |
| 23 | ||
| 24 | #======================================================== | |
| 25 | ||
| 26 | def linear_search(arr, x): | |
| 27 | n = len(arr) | |
| 28 | for i in range(n): | |
| 29 | if arr[i] == x: | |
| 30 | return i | |
| 31 | return -1 | |
| 32 | ||
| 33 | #======================================================== | |
| 34 | ||
| 35 | def binary_search(arr, x): | |
| 36 | low = 0 | |
| 37 | high = len(arr) - 1 | |
| 38 | mid = 0 | |
| 39 | ||
| 40 | while low <= high: | |
| 41 | ||
| 42 | mid = (high + low) // 2 | |
| 43 | ||
| 44 | if arr[mid] < x: | |
| 45 | low = mid + 1 | |
| 46 | ||
| 47 | elif arr[mid] > x: | |
| 48 | high = mid - 1 | |
| 49 | ||
| 50 | else: | |
| 51 | return mid | |
| 52 | ||
| 53 | return -1 | |
| 54 | ||
| 55 | #======================================================== | |
| 56 | ||
| 57 | # funkcja sortująca bubble sort | |
| 58 | def bubble_sort(arr): | |
| 59 | n = len(arr) | |
| 60 | for i in range(n): | |
| 61 | for j in range(0, n-i-1): | |
| 62 | if arr[j]["nazwisko"] > arr[j+1]["nazwisko"]: | |
| 63 | arr[j], arr[j+1] = arr[j+1], arr[j] | |
| 64 | return arr | |
| 65 | ||
| 66 | # lista kontaktów | |
| 67 | kontakty = [] | |
| 68 | ||
| 69 | # pętla programu | |
| 70 | while True: | |
| 71 | print("1. Dodaj nowy kontakt")
| |
| 72 | print("2. Wyświetl listę kontaktów")
| |
| 73 | print("3. Wyjdź z programu")
| |
| 74 | wybor = input("Wybierz opcję: ")
| |
| 75 | ||
| 76 | # dodawanie nowego kontaktu | |
| 77 | if wybor == "1": | |
| 78 | imie = input("Podaj imię: ")
| |
| 79 | nazwisko = input("Podaj nazwisko: ")
| |
| 80 | numer = input("Podaj numer telefonu: ")
| |
| 81 | kontakt = {"imie": imie, "nazwisko": nazwisko, "numer": numer}
| |
| 82 | kontakty.append(kontakt) | |
| 83 | kontakty = bubble_sort(kontakty) | |
| 84 | print("Kontakt został dodany.")
| |
| 85 | ||
| 86 | # wyświetlanie listy kontaktów | |
| 87 | elif wybor == "2": | |
| 88 | if len(kontakty) == 0: | |
| 89 | print("Brak kontaktów.")
| |
| 90 | else: | |
| 91 | print("Lista kontaktów:")
| |
| 92 | for kontakt in kontakty: | |
| 93 | print(f"{kontakt['nazwisko']} {kontakt['imie']} - {kontakt['numer']}")
| |
| 94 | ||
| 95 | # wyjście z programu | |
| 96 | elif wybor == "3": | |
| 97 | break | |
| 98 | ||
| 99 | # błąd - nieznana opcja | |
| 100 | else: | |
| 101 | print("Nieznana opcja.")
| |
| 102 | ||
| 103 | #======================================================== | |
| 104 | ||
| 105 | import random | |
| 106 | ||
| 107 | def binary_search(low, high, guess): | |
| 108 | mid = (low + high) // 2 | |
| 109 | print("Czy to jest liczba", guess, "?")
| |
| 110 | response = input("Podaj odpowiedź (za mało/za dużo/tak): ")
| |
| 111 | if response == "za mało": | |
| 112 | return binary_search(mid + 1, high, (mid + 1 + high) // 2) | |
| 113 | elif response == "za dużo": | |
| 114 | return binary_search(low, mid - 1, (low + mid - 1) // 2) | |
| 115 | else: | |
| 116 | return guess | |
| 117 | ||
| 118 | # losujemy liczbę z zakresu 1-100 | |
| 119 | number = random.randint(1, 100) | |
| 120 | print("Wylosowana liczba to", number)
| |
| 121 | ||
| 122 | # komputer zgaduje | |
| 123 | guess = 50 | |
| 124 | result = binary_search(1, 100, guess) | |
| 125 | print("Zgadłem! Wylosowana liczba to", result) |