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 |
| 14 | + | # iterate through all the elements in the list |
| 15 | for i in range(n): | |
| 16 | - | # ostatnie i elementów są już posortowane |
| 16 | + | # the last i elements are already sorted |
| 17 | for j in range(0, n-i-1): | |
| 18 | - | # porównujemy sąsiednie elementy |
| 18 | + | # we compare adjacent elements |
| 19 | if arr[j] > arr[j+1]: | |
| 20 | - | # zamieniamy miejscami, jeśli kolejność jest nieprawidłowa |
| 20 | + | # we swap the elements if the order is incorrect |
| 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 |
| 57 | + | # bubble sort function |
| 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"]: |
| 62 | + | if arr[j]["surname"] > arr[j+1]["surname"]: |
| 63 | arr[j], arr[j+1] = arr[j+1], arr[j] | |
| 64 | return arr | |
| 65 | ||
| 66 | - | # lista kontaktów |
| 66 | + | # contact list |
| 67 | - | kontakty = [] |
| 67 | + | contacts = [] |
| 68 | ||
| 69 | - | # pętla programu |
| 69 | + | # program loop |
| 70 | while True: | |
| 71 | - | print("1. Dodaj nowy kontakt")
|
| 71 | + | print("1. Add new contact ")
|
| 72 | - | print("2. Wyświetl listę kontaktów")
|
| 72 | + | print("2. Display the contact list ")
|
| 73 | - | print("3. Wyjdź z programu")
|
| 73 | + | print("3. Exit the program")
|
| 74 | - | wybor = input("Wybierz opcję: ")
|
| 74 | + | choice = input("Select an option: ")
|
| 75 | ||
| 76 | - | # dodawanie nowego kontaktu |
| 76 | + | # adding a new contact |
| 77 | - | if wybor == "1": |
| 77 | + | if choice == "1": |
| 78 | - | imie = input("Podaj imię: ")
|
| 78 | + | name = input("Enter a name: ")
|
| 79 | - | nazwisko = input("Podaj nazwisko: ")
|
| 79 | + | surname = input("Enter a surname: ")
|
| 80 | - | numer = input("Podaj numer telefonu: ")
|
| 80 | + | number = input("Enter a phone number: ")
|
| 81 | - | kontakt = {"imie": imie, "nazwisko": nazwisko, "numer": numer}
|
| 81 | + | contact = {"name": name, " surname": surname, "number": number}
|
| 82 | - | kontakty.append(kontakt) |
| 82 | + | contacts.append(contact) |
| 83 | - | kontakty = bubble_sort(kontakty) |
| 83 | + | contacts = bubble_sort(contacts) |
| 84 | - | print("Kontakt został dodany.")
|
| 84 | + | print("A new contact was added.")
|
| 85 | ||
| 86 | - | # wyświetlanie listy kontaktów |
| 86 | + | # displaying the contact list |
| 87 | - | elif wybor == "2": |
| 87 | + | elif choice == "2": |
| 88 | - | if len(kontakty) == 0: |
| 88 | + | if len(contacts) == 0: |
| 89 | - | print("Brak kontaktów.")
|
| 89 | + | print("No contacts.")
|
| 90 | else: | |
| 91 | - | print("Lista kontaktów:")
|
| 91 | + | print("Contact list:")
|
| 92 | - | for kontakt in kontakty: |
| 92 | + | for contact in contacts: |
| 93 | - | print(f"{kontakt['nazwisko']} {kontakt['imie']} - {kontakt['numer']}")
|
| 93 | + | print(f"{contact['surname']} {contact['name']} - {contact['number']}")
|
| 94 | ||
| 95 | - | # wyjście z programu |
| 95 | + | # exit the program |
| 96 | - | elif wybor == "3": |
| 96 | + | elif choice == "3": |
| 97 | break | |
| 98 | ||
| 99 | - | # błąd - nieznana opcja |
| 99 | + | # error - unknown option |
| 100 | else: | |
| 101 | - | print("Nieznana opcja.")
|
| 101 | + | print("Unknown option.")
|
| 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, "?")
|
| 109 | + | print("Is it the number", guess, "?")
|
| 110 | - | response = input("Podaj odpowiedź (za mało/za dużo/tak): ")
|
| 110 | + | response = input("Enter your answer (too small/too large/yes):")
|
| 111 | - | if response == "za mało": |
| 111 | + | if response == "too small": |
| 112 | return binary_search(mid + 1, high, (mid + 1 + high) // 2) | |
| 113 | - | elif response == "za dużo": |
| 113 | + | elif response == "too large": |
| 114 | return binary_search(low, mid - 1, (low + mid - 1) // 2) | |
| 115 | else: | |
| 116 | return guess | |
| 117 | ||
| 118 | - | # losujemy liczbę z zakresu 1-100 |
| 118 | + | # draw a number in the range 1-100 |
| 119 | number = random.randint(1, 100) | |
| 120 | - | print("Wylosowana liczba to", number)
|
| 120 | + | print("The number drawn is ", number)
|
| 121 | ||
| 122 | - | # komputer zgaduje |
| 122 | + | # the computer guesses |
| 123 | guess = 50 | |
| 124 | result = binary_search(1, 100, guess) | |
| 125 | - | print("Zgadłem! Wylosowana liczba to", result) |
| 125 | + | print("I guessed! The number drawn is ", result)
|
| 126 |