Repository navigation
Expand file tree
/
Copy path33_excercise_python.py
More file actions
208 lines (166 loc) · 5.37 KB
/
Copy path33_excercise_python.py
File metadata and controls
208 lines (166 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# # 1. Robot FizzBuzz.
# for n in range(1, 31):
# if n % 5 == 0 and n % 3 == 0:
# print("robot jalan")
# elif n % 3 == 0:
# print("robot")
# elif n % 5 == 0:
# print("jalan")
# else:
# print(n)
# # 2. hitung_faktorial(n)
# def hitung_faktorial(angka):
# hasil = 1
# for i in range(1, angka + 1):
# hasil *= i
# return hasil
# print(hitung_faktorial(5))
# # 3. statistik manual
# data = [80, 45, 92, 67, 30]
# def cari_max(angka):
# hasil = angka[0]
# for n in angka:
# if n > hasil:
# hasil = n
# return hasil
# def cari_min(angka):
# hasil = angka[0]
# for n in angka:
# if n < hasil:
# hasil = n
# return hasil
# def cari_sum(angka):
# hasil = 0
# for n in angka:
# hasil += n
# return hasil
# print(cari_max(data))
# print(cari_min(data))
# print(cari_sum(data))
# # 4. penghitung frekuensi karakter
# teks = "robotika"
# hitungan = {}
# for huruf in teks:
# if huruf in hitungan:
# hitungan[huruf] += 1
# else:
# hitungan[huruf] = 1
# print(hitungan)
# # 5. Class RekeningBank
# class RekeningBank:
# def __init__ (self, nama, saldo):
# self.nama = nama
# self.saldo = saldo
# self.riwayat = []
# def setor(self, jumlah):
# self.saldo += jumlah
# self.riwayat.append(f"Disetorkan uang sebesar {jumlah}")
# def tarik(self, jumlah):
# if jumlah > self.saldo:
# print("Saldo anda tidak mencukupi")
# else:
# self.saldo -= jumlah
# self.riwayat.append(f"Ditarik uang sebesar {jumlah}")
# def cek_saldo(self):
# print(f"A.N {self.nama}, sisa saldo {self.saldo}")
# print(f"riwayat: {self.riwayat}")
# def __str__(self):
# return f"A.N {self.nama}, sisa saldo {self.saldo}"
# atm1 = RekeningBank("Yasin", 1000000)
# atm1.tarik(10000000)
# atm1.cek_saldo()
# # 6. Cek baterai robot
# class Robot:
# def __init__ (self, nama, baterai):
# self.nama = nama
# self.baterai = baterai
# self.status = "idle"
# self.riwayat = []
# def jalan(self, langkah):
# self.baterai -= langkah
# self.status = "bergerak"
# self.riwayat.append(f"jalan {langkah} langkah")
# def lapor(self):
# print(f"{self.nama} | baterai {self.baterai}% | status: {self.status}")
# print(f"riwayat: {self.riwayat}")
# def __str__ (self):
# return f"Robot {self.nama} (baterai {self.baterai}%, {self.status})"
# def hitung_baterai_rendah(daftar):
# jumlah = 0
# for robot in daftar:
# if robot.baterai < 20:
# jumlah += 1
# return jumlah
# daftar_robot = [Robot("R1", 100), Robot("R2", 67), Robot("R3", 30),
# Robot("R4", 99), Robot("R5", 80), Robot("R6", 10), Robot("R7", 5)]
# print(hitung_baterai_rendah(daftar_robot))
# 7. manipulasi matriks 5x5
# import numpy as np
# m = np.array([[1, 2, 3, 4, 5],
# [6, 7, 8, 9, 10],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25]])
# print("A =", m[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]])
# print("B =", m[1:4, 1:4])
# print("C =", m[[0, 4]])
# # 8. Papan catur 8x8
# import numpy as np
# import matplotlib.pyplot as plt
# gambar = np.array([
# [0, 255, 0, 255,0, 255,0, 255],
# [255, 0, 255, 0, 255, 0, 255, 0, ],
# [0, 255, 0, 255,0, 255,0, 255],
# [255, 0, 255, 0, 255, 0, 255, 0, ],
# [0, 255, 0, 255,0, 255,0, 255],
# [255, 0, 255, 0, 255, 0, 255, 0, ],
# [0, 255, 0, 255,0, 255,0, 255],
# [255, 0, 255, 0, 255, 0, 255, 0, ],
# ])
# # Cara 1: bikin kosong, isi pola pakai slicing berpola
# papan = np.zeros((8, 8)) # semua 0 (hitam) dulu
# papan[::2, 1::2] = 255 # baris genap, kolom ganjil → putih
# papan[1::2, ::2] = 255 # baris ganjil, kolom genap → putih
# # Cara 2: trik matematika (lebih ringkas, agak advanced)
# i, j = np.indices((8, 8))
# papan_mtk = (i + j) % 2 * 255 # (baris+kolom) genap=0, ganjil=255
# plt.imshow(papan_mtk, cmap="gray", vmin=0, vmax=255)
# plt.show()
# 9. To Do List dengan file
class Tugas:
def __init__ (self, teks):
self.teks = teks
self.selesai = False
def tandai_selesai(self):
self.selesai = True
def __str__(self):
tanda = "[x]" if self.selesai else "[ ]"
return f"{tanda} {self.teks}"
daftar_tugas = []
while True:
print("\n=== TO-DO LIST ===")
print("1. Tambah tugas")
print("2. Lihat semua tugas")
print("3. Tandai selesai")
print("4. Keluar")
pilihan = input("Pilih (1-4): ")
if pilihan == "1":
teks = input("Tugas baru: ")
daftar_tugas.append(Tugas(teks))
print("Tugas ditambah!")
elif pilihan == "2":
for tugas in daftar_tugas:
print(tugas)
elif pilihan == "3":
for i in range(len(daftar_tugas)):
print(f"{i + 1}. {daftar_tugas[i]}")
nomor = int(input("Nomor tugas yg selesai: "))
daftar_tugas[nomor - 1].tandai_selesai()
elif pilihan == "4":
with open("tugas.txt", "w") as file:
for tugas in daftar_tugas:
file.write(str(tugas) + "\n")
print("Tugas disimpan! Sampai jumpa!")
break
else:
print("Pilihan tidak valid!")