Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
from os import system
import sys

def clear_print():
    system('clear')
    

def printer():
    global set_ctr
    clear_print()
    print("SET {0} ALLOCATION".format(set_ctr))

    print("\nMEMORY\t\t\tPARTITION SIZE\tALLOC\\DEALLOC\n""OS Partition\t{0:g}\t\t\t\tOS".format(OS_size))
    ctr = 1
    for item in partitions:
        if ctr >= 10:
            if allocations.get(item) is None:
                print("{0}\t{1:g}".format(item, partitions[item]))
            else:
                print("{0}\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))
        else:
            ctr += 1
            if allocations.get(item) is None:
                print("{0}\t\t{1:g}".format(item, partitions[item]))
            else:
                print("{0}\t\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))

def allocation():
    global set_ctr
    available_storage = memory_size - sum(partitions.values(), OS_size)
    partition_counter = 1
    partition_number = "Partition {0}".format(partition_counter)
    for i in list(jobs.keys()):
        if jobs[i] <= available_storage and i not in currently:
            partitions[partition_number] = jobs[i]
            allocations[partition_number] = jobs[i]
            partition_job[partition_number] = i
            currently.append(i)
            partition_counter += 1
            partition_number = "Partition {0}".format(partition_counter)
            available_storage = memory_size - sum(partitions.values(), OS_size)
    if available_storage != 0:
        partitions[partition_number] = available_storage

    if len(allocations) != 0:
        printer()
        input("\nPress any key to continue...")
        set_ctr += 1
        currently.clear()
        deallocation()

def deallocation():
    for i in list(partition_job.values()):
        for j in list(partition_job.keys()):
            if partition_job[j] == i:
                tat[i] = tat[i] - 1
                if tat[i] == 0:
                    allocations.pop(j)
                    partition_job.pop(j)
                    jobs.pop(i)
    partitions.clear()
    partition_job.clear()
    allocations.clear()
    allocation()

def validator(string, num):
    if is_number(num):
        while float(num) <= 0:
            sys.stdout.write("\033[F")
            sys.stdout.write("\033[K")
            num = validator(string, input(string))
    else:
        sys.stdout.write("\033[F")
        sys.stdout.write("\033[K")
        num = validator(string, input(string))
    return float(num)

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

clear_print()
memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))

partitions = dict()
jobs = dict()
tat = dict()
allocations = dict()
partition_job = dict()
currently = []

set_ctr = 1
exit_ctr = 0

# Validates OS and memory size
while OS_size >= memory_size:
    print("OS size cannot be greater than or equal to memory size.")
    input("\nPress any key to continue...")
    clear_print()
    memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
    OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))

# Gets jobs
clear_print()
for x in range(10):
    jobs["Job {0}".format(x + 1)] = validator("Enter Job {0}: ".format(x + 1), input("Enter Job {0}: ".format(x + 1)))
    tat["Job {0}".format(x + 1)] = validator("Enter TAT for Job {0}: ".format(x + 1), input("Enter TAT for Job {0}: ".format(x + 1)))
    print()
input("\nPress any key to continue...")

# Prints jobs list
clear_print()
print("JOB LIST\n")
for x in range(len(jobs)):
    job = "Job {0}".format(x + 1)
    print("{0}: {1:g}\tTAT:{2:g}".format(job, jobs[job], tat[job]))
input("\nPress any key to continue...")

allocation()
deallocation()

# Prints the conclusion
clear_print()
print("There were {0} set(s) of allocation.".format(set_ctr - 1))
if len(jobs) == 0:
    input("All jobs were served/executed.\n\nPress any key to continue...")
else:
    if len(jobs) == 1:
        print("{0}".format(list(jobs.keys())[0]), end=" ")
    else:
        for jobs_item in jobs:
            if jobs_item == list(jobs.keys())[-1]:
                print("and {0}".format(jobs_item), end=" ")
            else:
                print(jobs_item, end=", ")
    input("were not served/executed.\n\nPress any key to continue...")


What I have tried:

Have difficulty to understand and convert it. Please help me
Posted
Updated 10-Feb-23 1:22am
v4
Comments
CHill60 5-Jul-21 9:30am    
How can we help you when you have deleted the text of the question? Please do not do that.

Simple:

1) Learn Python;
2) Read and understand what that code is doing;
3) Learn C#;
4) Rewrite the code in C#;

NB: This is NOT a code conversion service. If you were expecting anyone here to convert that code for you, or even to provide a detailed explanation of what that code is doing, you've come to the wrong site.
 
Share this answer
 
Comments
Maciej Los 5-Jul-21 8:57am    
5ed!
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 
Comments
Maciej Los 5-Jul-21 8:58am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900