Hai, sudah hampir 2 minggu setelah aku menulis blog terakhirku. Sekarang aku baru menyelesaikan tugas dari sekolah. Menurutku tugas ini menarik karena aku harus mencari jumlah waktu yang dierlukan oleh pengguna untuk mengetik beberapa kata.
Program yang ditulis dalam python ini adalah program untuk menguji kecepatan mengetik seseorang. Kode ini menggunakan beberapa string berisi kata-kata. Hasil dianalisa untuk setiap string yang digunakan, tetapi ada rata-ratanya juga.
Di awal, aku meng-import modul time. Ini berguna untuk mengetahui berapa lama waktu yang diperlukan pengguna untuk mengetik kata-kata yang diberikan:
import time as tmod
Dalam hal ini, modul di-import dengan nama 'tmod'. Hal ini karena nantinya akan ada variabel time, yang akan ber-interferensi dengan modul ini. Kemudian aku membuat sebuah list berisi string-string yang akan digunakan:
strings = ["Watching, we hear the mad gusts tugging on the wire, Like twitching agonies of men among its brambles.",\
"Our brains ache, in the merciless iced east winds that knive us. Wearied we keep awake because the night is silent",\
"Worried by silence, sentries whisper, curious, nervous, But nothing happens.",\
"Tonight, this frost will fasten on this mud and us, many hands, and puckering foreheads crisp."]
Karakter \ di akhir setiap baris memungkinkan pernyataan ditulis di beberapa baris, oleh sebab beberapa teks cukup panjang. Kemudian, inisialisasi variabel untuk menghitung hasil rata-rata:
gtime = 0
gaccuracy = 0
gWPM = 0
tambahan 'g' di awal membedakan mereka dengan variabel yang menyimpan hasil untuk satu teks. Setelah itu dibuat sebuah loop untuk semua teks yang digunakan:
for string in string:
Kemudian, program akan menunggu sampai pengguna menekan enter:
input("Press enter to start")
Hasil dari pemangilan fungsi input dapat diabaikan. Lalu, petunjuk untuk pengguna akan ditampilkan, berikut dengan teks yang sedang digunakan.
print("Type the following lines as fast as possible\n(do NOT correct it: corrections can lead to inaccurate results)\nNB: Pay attention to captialization (e.g. A is different with a) and punctuation\n\n")
print(string)
Setelah itu, waktu sebelum dan setelah pengguna mengetik baris tersebut, dan teks yang diketikan disimpan:
begin = tmod.perf_counter()
result = input()
end = tmod.perf_counter()
Metode perf_counter() dari modul time merekam waktu yang didapat dari clock dengan frequensi tertinggi. Dalam kata lain, metode ini mencatat waktu terakurat yang tersedia di sistem. Kemudian, hasil dianalisa. Waktu yang dibutuhkan dihitung oleh program:
time = end - begin
Selisih dari kedua waktu yang dicatat saat pengetikan adalah lamanya waktu yang pengguna gunakan untuk mengetik teks. Lalu, WPM (word per minute) mulai dihitung. Berikut rumusnya:
Sekarang, dihitung bagian yang berurusan dengan banyak karakter (bagian yang melibatkan kesalahan akan dihitung setelah akurasi dihitung). WPM = 12 * len(result)/time
Ini adalah hasil penyederhanaan persamaan diatas (untuk bagian dengan banyak karakter). Kemudian akurasi dihitung. Algoritmanya mengecek setiap kata agar kesalahan di sebuah kata tidak menganggu pengecekan kata-kata selanjutnya:
accuracy = 0
entries = min(len(string.split()), len(result.split()))
for i in range(entries):
newstring = string.split()
newresult = result.split()
newentries = min(len(newstring[i]), len(newresult[i]))
for j in range(newentries):
if (newresult[j]) == newstring[j]:
accuracy += 1
entries = min(len(string), len(result))
wrongs = entries - accuracy
accuracy /= entries
accuracy *= 100
Disini, wrongs menyimpan banyaknya karakter yang salah, dan bagian yang melibatkan kesalahan akan dihitung:
WPM -= wrongs * 60 / time
Ini juga sudah disederhanakan, seperti di kalkulasi awal. kemudian hasil-hasilnya dimasukan ke data global dan hasil untuk teks ini dicetak
gtime += time
gaccuracy += accuracy
gWPM += WPM
print("Time(seconds) :", time)
print("Accuracy (%) :", accuracy)
print("WPM (words per minute):", WPM)
Lalu, di akhir hasil rata-rata dicetak juga:
length = len(strings)
print("\n\n\n=====================AVERAGE RESULT=====================")
print("Time(seconds) :", gtime/length)
print("Accuracy (%) :", gaccuracy/length)
print("WPM (words per minute):", gWPM/length)
Berikut kode utuhnya:
import time as tmod
print("=======================")
print(" Typing Speed Test ")
print("=======================")
print("Created by Ackhava")
print("Written in python at 25032021\n\n\n")
strings = ["Watching, we hear the mad gusts tugging on the wire, Like twitching agonies of men among its brambles.",\
"Our brains ache, in the merciless iced east winds that knive us. Wearied we keep awake because the night is silent",\
"Worried by silence, sentries whisper, curious, nervous, But nothing happens.",\
"Tonight, this frost will fasten on this mud and us, many hands, and puckering foreheads crisp.",\
]
gtime = 0
gaccuracy = 0
gWPM = 0
for string in strings:
input("Press enter to start:")
print("Type the following lines as fast as possible\n(do NOT correct it: corrections can lead to inaccurate results)\nNB: Pay attention to captialization (e.g. A is different with a) and punctuation\n\n")
print(string)
begin = tmod.perf_counter()
result = input()
end = tmod.perf_counter()
time = end - begin
WPM = 12 * len(result)/time
accuracy = 0
entries = min(len(string.split()), len(result.split()))
for i in range(entries):
newstring = string.split()
newresult = result.split()
newentries = min(len(newstring[i]), len(newresult[i]))
for j in range(newentries):
if (newresult[j]) == newstring[j]:
accuracy += 1
entries = min(len(string), len(result))
wrongs = entries - accuracy
accuracy /= entries
accuracy *= 100
WPM -= wrongs * 60 / time
gtime += time
gaccuracy += accuracy
gWPM += WPM
print("Time(seconds) :", time)
print("Accuracy (%) :", accuracy)
print("WPM (words per minute):", WPM)
length = len(strings)
print("\n\n\n=====================AVERAGE RESULT=====================")
print("Time(seconds) :", gtime/length)
print("Accuracy (%) :", gaccuracy/length)
print("WPM (words per minute):", gWPM/length)
Berikut contoh screenshot kodenya:
=======================
Typing Speed Test
=======================
Created by Ackhava
Written in python at 25032021
Press enter to start:
Type the following lines as fast as possible
(do NOT correct it: corrections can lead to inaccurate results)
NB: Pay attention to captialization (e.g. A is different with a) and punctuation
Watching, we hear the mad gusts tugging on the wire, Like twitching agonies of men among its brambles.
Watching, we hear the mad gusts tugging on the wire, Like twitching agonis of men among its brambles.
Time(seconds) : 15.612113399999998
Accuracy (%) : 99.00990099009901
WPM (words per minute): 73.78885679884955
Press enter to start:
Type the following lines as fast as possible
(do NOT correct it: corrections can lead to inaccurate results)
NB: Pay attention to captialization (e.g. A is different with a) and punctuation
Our brains ache, in the merciless iced east winds that knive us. Wearied we keep awake because the night is silent
Our brains ache, in the merciless iced east winds that knive us. Wearied we keep awake because the night is silents
Time(seconds) : 22.8390048
Accuracy (%) : 100.0
WPM (words per minute): 60.42294802617669
Press enter to start:
Type the following lines as fast as possible
(do NOT correct it: corrections can lead to inaccurate results)
NB: Pay attention to captialization (e.g. A is different with a) and punctuation
Worried by silence, sentries whisper, curious, nervous, But nothing happens.
Worried by silence, sentries whisper, curious, nervous, But nothing happens.
Time(seconds) : 14.528717499999992
Accuracy (%) : 100.0
WPM (words per minute): 62.77223023986807
Press enter to start:
Type the following lines as fast as possible
(do NOT correct it: corrections can lead to inaccurate results)
NB: Pay attention to captialization (e.g. A is different with a) and punctuation
Tonight, this frost will fasten on this mud and us, many hands, and puckering foreheads crisp.
Tonight, this frost will tasten on this mud and us, many hands, and puckering forhands crisp.
Time(seconds) : 21.000848599999998
Accuracy (%) : 93.54838709677419
WPM (words per minute): 35.99854531592595
=====================AVERAGE RESULT=====================
Time(seconds) : 18.495171074999995
Accuracy (%) : 98.13957202171831
WPM (words per minute): 58.24564509520506
Ya... Itu saja yang bisa aku jelaskan untuk program ini. Sampai berjumpa lagi!