Skip to content

Commit

Permalink
Refactoring Code With Classes for Ease of Modification
Browse files Browse the repository at this point in the history
  • Loading branch information
brotherzhafif committed Oct 12, 2024
1 parent e9186ac commit 34c6c99
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 76 deletions.
69 changes: 0 additions & 69 deletions Frequency.py

This file was deleted.

89 changes: 89 additions & 0 deletions FrequencyTable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import numpy as np

class FrequencyTable:
def __init__(self, dataset):
# Data Initiation
self.dataset = sorted(dataset)
self.amount = len(dataset)
self.lowest = min(dataset)
self.highest = max(dataset)

# Counting Data Range
self.range = self.highest - self.lowest

# Classes is Rounding Down
# Math Log Base 10 In Python For Accurate Result
self.classes = 1 + (3.222 * np.log10(self.amount))
self.classes = round(self.classes - 0.5)

# Interval is Rounding Up
self.interval = self.range / self.classes
self.interval = round(self.interval + 0.5)

# Rounding Both Limit So The Data Would Be Simple And Easier To Read
self.base = self.roundy(self.lowest - 3)
self.top = self.roundy(self.highest + 3)

def Populate(self):
# Initiating Used List
lower = []
upper = []
frequency = []
data_range = []
data_limit = []
data_midpoint = []

# Initiating Used Parameter
interval = self.interval # 4
current_number = self.base - 1 # 156
old_number = 0

while current_number <= self.top-3:
# Finding Botom Limit Data
old_number = current_number + 1
lower.append(old_number) # 155

# Finding Top Limit Data
current_number = current_number + interval
upper.append(current_number)

# Finding The Frequency That Range
range_frequency = self.find_frequency(old_number, current_number)
frequency.append(range_frequency)

# Adding The Number Range From Both Frequency
data_range.append(str(old_number) + " ~ " + str(current_number))

# Adding Data Range Limit Of The Class Frequency
data_limit.append(str(old_number - 0.5) + " ~ " + str(current_number + 0.5))

# Assign Value Each Processed Data
self.bottom_limit = lower
self.top_limit = upper
self.frequency = frequency
self.ranges = data_range
self.limit = data_limit

# Append Processed Data into Data Attributes
self.data = ProcessedData(self.bottom_limit, self.top_limit, self.frequency, self.ranges, self.limit)

# Base 5 Rounding
def roundy(self, x, base = 5):
return base * round(x/base)

# Function To Find Frequency in Dataset with Desired Range (Top and Down Limit)
def find_frequency(self, bot, top):
total_frequency = 0
for i in range(bot, top + 1):
frequency = self.dataset.count(i)
total_frequency = total_frequency + frequency
return total_frequency

class ProcessedData:
def __init__(self, bottoms, tops, frequency, ranges, limit):
self.bottom = bottoms
self.top = tops
self.frequency = frequency # frekuensi terproses
self.ranges = ranges # rentang terproses
self.limit = limit # batas kelas terproses

22 changes: 15 additions & 7 deletions Main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import Frequency as fr
import Chart
import FrequencyTable as ft

# Misalnya, Anda punya data mentah seperti ini:
data = (
58, 67, 45, 89, 72, 60, 76, 93,
55, 48, 62, 85, 79, 56, 41, 90,
77, 54, 68, 82, 46, 73, 57, 92,
81, 53, 66, 74, 64, 52, 91, 78,
49, 87, 88, 50, 69, 84, 43, 65,
83, 70, 44, 61, 75, 80, 71, 63, 47,51)

# Main Control For Statistic Tools
Range_limit = fr.data_range
Frequency_limit = fr.data_frequency
# Membuat objek dari FrequencyTable dengan data mentah
table = ft.FrequencyTable(data)

print(Range_limit)
print(Frequency_limit)
# Memproses data mentah menjadi tabel frekuensi
table.Populate()

print(table.data.ranges) # Output: [0, 10, 20, 30] (data terproses)
print(table.data.frequency) # Output: [10, 20, 30, 40] (data terproses)
Binary file added __pycache__/Chart.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/Dataset.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/Frequency.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/frequency_table.cpython-38.pyc
Binary file not shown.

0 comments on commit 34c6c99

Please sign in to comment.