Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.testing.unittestArgs": ["-v", "-s", "./Chapter01", "-p", "*test.py"],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
48 changes: 15 additions & 33 deletions Chapter01/dir.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
# Dalam program ini,
# kami memeriksa apakah angkanya positif atau negatif atau nol dan
# tampilkan pesan yang sesuai

num = 1

# Try these two variations as well:
# num = 0
# num = -4.5

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
masuk=int(input("Masukkan Jam Masuk = "))
keluar=int(input("Masukkan Jam Keluar ="))
lama=keluar-masuk
payment=12000
print("Lama Mengajar = ", lama, "jam")
if lama <=1:
satu_jam_pertama=payment
print("Biaya Mengajar= Rp", satu_jam_pertama)
elif lama <10:
biaya_selanjutnya = (lama+1)*3000+payment
print("Biaya Mengajar = Rp", biaya_selanjutnya)
elif lama >= 10:
print("Biaya Mengajar = Rp", 1000000)
else:
print("Negative number")



# Program to find the sum of all numbers stored in a list

# List of numbers
numbers = [6, 6, 3, 8, -3, 2, 5, 44, 12]

# variable to store the sum
sum = 0

# iterate over the list
for val in numbers:
sum = sum+val

# Output: The sum is 48
print("The sum is", sum)

print("nul")

49 changes: 21 additions & 28 deletions Chapter01/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@
# In this program, we check if the number is positive or negative or zero and
# display an appropriate message

num = 1
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

# Flow Control

# FOR
# Program to find the sum of all numbers stored in a list
numbers = [6, 6, 3, 8, -3, 2, 5, 44, 12]
sum = 0
for val in numbers:
sum = sum+val

# Output: The sum is 48
print("The sum is", sum)
x=7
if x>10:
print("x is big.")
elif x > 0:
print("x is small.")
else:
print("x is not positive.")

# Array

#WHILE
# Program to add natural numbers upto sum = 1+2+3+...+n
# Variabel array
genap = [14,24,56,80]
ganjil = [13,55,73,23]

n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
nap = 0
jil = 0

# print the sum
print("The sum is", sum)
# Buat looping for menggunakanvariable dari array yang udah dibuat
for val in genap:
nap = nap+val
for val in ganjil:
jil = jil+val

print("Ini adalah bilangan Genap", nap )
print("Ini adalah bilangan Ganjil", jil )
16 changes: 16 additions & 0 deletions Chapter01/studiKasus/dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
masuk=int(input("Masukkan Jam Masuk = "))
keluar=int(input("Masukkan Jam Keluar ="))
lama=keluar-masuk
payment=12000
print("Lama Mengajar = ", lama, "jam")
if lama <=1:
satu_jam_pertama=payment
print("Biaya Mengajar= Rp", satu_jam_pertama)
elif lama <10:
biaya_selanjutnya = (lama+1)*3000+payment
print("Biaya Mengajar = Rp", biaya_selanjutnya)
elif lama >= 10:
print("Biaya Mengajar = Rp", 1000000)
else:
print("nul")

22 changes: 22 additions & 0 deletions Chapter01/studiKasus/filepasiencovid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#melakukan percobaan mengimplementasi Input pada file
f = open ('inputPasienCovid.txt', 'w') #membaca file di luar
nik = input ("Masukan NIK :")
nama = input("Masukan Nama : ")
alamat = input("Masukan Alamat : ")
umur = input("Masukan Usia : ")
jeniskelamin = input("Masukan Jenis Kelamin: ")
nohp = input("Masukan Nohp : ")
gejala = input("Masukan Gejala : ")
f.write ("NIK : " + nik + "\n") #menulis ke dalam file
f.write ("Nama : " + nama + "\n")
f.write ("Alamat : " + alamat + "\n")
f.write ("Umur : " + umur + "\n")
f.write ("Jenis Kelamin : " + jeniskelamin + "\n")
f.write ("No Hp : " + nohp + "\n")
f.write ("Gejala : " + gejala + "\n")
f.close()#keluarkan file nya
f = open ('inputPasienCovid.txt') #buka file
content = f.read()
print (content) #render content yang ada di dalam file nya

f.close()
33 changes: 33 additions & 0 deletions Chapter01/studiKasus/flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# IF

# In this program, we check if the number is positive or negative or zero and
# display an appropriate message

# Flow Control

x=7
if x >10:
print("x is the value is big.")
elif x > 0:
print("x the value is small.")
else:
print("x is not positive.")

# Array

# Variabel array
genap = [14,24,56,80]
ganjil = [13,55,73,23]

nap = 0
jil = 0

# Buat looping for menggunakanvariable dari array yang udah dibuat
for val in genap:
nap = nap+val
for val in ganjil:
jil = jil+val

print("Ini adalah bilangan Genap", nap )
print("Ini adalah bilangan Ganjil", jil )
14 changes: 14 additions & 0 deletions Chapter01/studiKasus/listjenistumbuhan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bunga =["Angrek", "Mawar", "Melati", "Kamboja", "Teratai", "Lily", "Tulip", "Kertas", "Asoka", "Bougenville", "Kemuning "]
print(bunga)
print(bunga[0])
print(bunga[6:9])
bunga[0] = "Anggrek Ungu"
bunga[1] = "Mawar Putih"
print(bunga)
KeteranganMawarPutih ={"Nama":"Mawar Putih"}
print(KeteranganMawarPutih)
pohon = ("Mangga", "Nangka", "Rambutan", "Jambu Batu", "Jambu Air", "Pisang", "Duren")
print(pohon)
mengukurPanjangData = len
print(mengukurPanjangData(bunga))
print(mengukurPanjangData(pohon))
12 changes: 6 additions & 6 deletions Chapter02/Event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
event = threading.Event()


class Consumer(threading.Thread):
class Pasien(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -19,10 +19,10 @@ def run(self):
time.sleep(2)
event.wait()
item = items.pop()
logging.info('Consumer notify: {} popped by {}'\
logging.info('Pasien notify: pasien {} menuju ruang dokter {}'\
.format(item, self.name))

class Producer(threading.Thread):
class Dokter(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -31,14 +31,14 @@ def run(self):
time.sleep(2)
item = random.randint(0, 100)
items.append(item)
logging.info('Producer notify: item {} appended by {}'\
logging.info('Dokter notify: dolter memeriksa pasien {}'\
.format(item, self.name))
event.set()
event.clear()

if __name__ == "__main__":
t1 = Producer()
t2 = Consumer()
t1 = Pasien()
t2 = Dokter()

t1.start()
t2.start()
Expand Down
32 changes: 7 additions & 25 deletions Chapter02/MyThreadClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from random import randint
from threading import Thread

class MyThreadClass (Thread):
class MyPasien (Thread):
def __init__(self, name, duration):
Thread.__init__(self)
self.name = name
self.duration = duration
def run(self):
print ("---> " + self.name + \
" running, belonging to process ID "\
" pasien diberikan no daftar "\
+ str(os.getpid()) + "\n")
time.sleep(self.duration)
print ("---> " + self.name + " over\n")
Expand All @@ -20,37 +20,19 @@ def main():
start_time = time.time()

# Thread Creation
thread1 = MyThreadClass("Thread#1 ", randint(1,10))
thread2 = MyThreadClass("Thread#2 ", randint(1,10))
thread3 = MyThreadClass("Thread#3 ", randint(1,10))
thread4 = MyThreadClass("Thread#4 ", randint(1,10))
thread5 = MyThreadClass("Thread#5 ", randint(1,10))
thread6 = MyThreadClass("Thread#6 ", randint(1,10))
thread7 = MyThreadClass("Thread#7 ", randint(1,10))
thread8 = MyThreadClass("Thread#8 ", randint(1,10))
thread9 = MyThreadClass("Thread#9 ", randint(1,10))
thread1 = MyPasien("no daftar pasien ", randint(1,10))
thread2 = MyPasien("no daftar pasien", randint(1,10))


# Thread Running
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()


# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()


# End
print("End")
Expand Down
44 changes: 14 additions & 30 deletions Chapter02/MyThreadClass_lock_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,53 @@
# Lock Definition
threadLock = threading.Lock()

class MyThreadClass (Thread):
def __init__(self, name, duration):
Thread.__init__(self)
class MyPasien (Thread):
def _init_(self, name, duration):
Thread._init_(self)
self.name = name
self.duration = duration
def run(self):
#Acquire the Lock
threadLock.acquire()
print ("---> " + self.name + \
" running, belonging to process ID "\
" pasien, ID pasiens "\
+ str(os.getpid()) + "\n")
threadLock.release()
time.sleep(self.duration)
print ("---> " + self.name + " over\n")
print ("---> " + self.name + " selesai\n")
#Release the Lock


def main():
start_time = time.time()

# Thread Creation
thread1 = MyThreadClass("Thread#1 ", randint(1,10))
thread2 = MyThreadClass("Thread#2 ", randint(1,10))
thread3 = MyThreadClass("Thread#3 ", randint(1,10))
thread4 = MyThreadClass("Thread#4 ", randint(1,10))
thread5 = MyThreadClass("Thread#5 ", randint(1,10))
thread6 = MyThreadClass("Thread#6 ", randint(1,10))
thread7 = MyThreadClass("Thread#7 ", randint(1,10))
thread8 = MyThreadClass("Thread#8 ", randint(1,10))
thread9 = MyThreadClass("Thread#9 ", randint(1,10))
thread1 = MyPasien("daftar pasien {}", randint(1,10))
thread2 = MyPasien("daftar pasien {}", randint(1,10))


# Thread Running
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()


# Thread joining
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()



# End
print("End")

#Execution Time
print("--- %s seconds ---" % (time.time() - start_time))
print("--- %s detik ---" % (time.time() - start_time))


if __name__ == "__main__":
main()





#class_lock2
Loading