diff --git a/6B_Group4.py b/6B_Group4.py new file mode 100644 index 0000000..5d076d3 --- /dev/null +++ b/6B_Group4.py @@ -0,0 +1,233 @@ +""" +Yasin Bey, + 1 - Görev Ekleme fonksiyonu + Fonksiyon adı: add_task + Parametreler: tasks (list), tasks_file (str) + tasks : Task listesi + task = { + "id": task_id, + "name": task_name, + "status": "Pending", "Completed", "Deleted" + } + tasks_file : Task listesinin kaydedileceği dosya adı + İşlev: Yeni bir görev ekler. + İşlem: + - Yeni bir görev oluşturulur ve tasks listesine eklenir. + - Görev name kullanıcı tarafından girilir. + - Yeni görevin id'si otomatik olarak atanır ve tasks listesindeki en büyük id'den bir fazla olacak şekilde oluşturulur. + - Yeni görevin status'u "Pending" olarak ayarlanır. + - task tasks listesine eklenir. + - tasks dosyaya kaydedilir. save_file fonksiyonu kullanılır. save_file(tasks_file, tasks) şeklinde kullanılır. + - Kullanıcıya görevin eklenmesi başarılı olduğu bilgisi verilir. + Dönüş Değeri: Boolean (True)or False kayıt başarılı ise True başarısız ise False döner. + 2 - Görev Tamamlama fonksiyonu + Fonksiyon adı: complete_task + Parametreler: tasks (list), task_id (int), tasks_file (str) + tasks : Task listesi + task_id : Tamamlanacak görevin id'si + tasks_file : Task listesinin kaydedileceği dosya adı + işlev: Bir görevin tamamlanmasını sağlar. + İşlem: + - task_id ile tasks listesinde arama yapılır. + - Eğer task_id ile eşleşen bir görev bulunamazsa, "Task not found" mesajı gösterilir ve fonksiyon sonlanır. + - Eğer task_id ile eşleşen bir görev bulunursa, görevin status'u "Completed" olarak güncellenir. + - tasks dosyaya kaydedilir. save_file fonksiyonu kullanılır. save_file(tasks_file, tasks) şeklinde kullanılır. + - Kullanıcıya görevin tamamlanması başarılı olduğu bilgisi verilir. + Dönüş Değeri: Boolean (True)or False kayıt başarılı ise True başarısız ise False döner. + Kahraman Bey, + 3 - Görev Silme fonksiyonu + Fonksiyon adı: delete_task + Parametreler: tasks (list), task_id (int), tasks_file (str) + tasks : Task Listesi + task_id : Silinecek görevin id'si + tasks_file : Task listesinin kaydedileceği dosya adı + İşlev: Bir görevin statusun deleted olarak güncellenmesini sağlar. + İşlem: + - task_id ile tasks listesinde arama yapılır. + - Eğer task_id ile eşleşen bir görev bulunamazsa, "Task not found" mesajı gösterilir ve fonksiyon sonlanır. + - Eğer task_id ile eşleşen bir görev bulunursa, görevin status'u "Deleted" olarak güncellenir. + - task_id değerine None atanır. + - task_id si silinen task_id den büyük olan görevlerin id'leri bir azaltılır. + - tasks dosyaya kaydedilir. save_file fonksiyonu kullanılır. save_file(tasks_file, tasks) şeklinde kullanılır. + - Kullanıcıya görevin silinmesi başarılı olduğu bilgisi verilir. + Dönüş Değeri: Boolean (True)or False kayıt başarılı ise True başarısız ise False döner. + Neslihan Hanım, + 4 - Listeleme fonksiyonu + Fonksiyon adı: list_tasks + Parametreler: tasks (list), status (list) + tasks : Task listesi + status : Görevlerin listelenmek istenen durumu örneğin status = ["Pending", "Completed"] + İşlev: Görevleri listeler ve bir grid formatında görüntüler. + İşlem: + - tasks listesi içerisinde status değeri status parametresinin içinde olan görevler filtrelenir. + - Filitrelenen görevler grid formatında görüntülenir. + - Görevlerin id, name ve status bilgileri görüntülenir. + Döüş Değeri: Listelediği görevlerin sayısını döner. + Mustafa Gundoğdu: + Ana program menüsü ile kullanıcıdan alınan seçeneklere göre fonksiyonları çağıran ve gerekli işlemleri yapan bir ana program yazınız. + +""" + +import json,os +def save_file(file_path, data): + try: + with open(file_path, 'w', encoding='utf-8') as file: + json.dump(data, file, ensure_ascii=False, indent=4) + # print(f"Data was successfully written to file: {file_path}") + except Exception as e: + print(f"Error: Data could not be written to file. Details: {e}") +def load_file(file_path): + """ + Reads a list from a file in JSON format. + """ + if not os.path.exists(file_path): + save_file(file_path, []) + return [] + try: + with open(file_path, 'r', encoding='utf-8') as file: + return json.load(file) + except json.JSONDecodeError: + print("Error: JSON format is invalid.") + return [] + except Exception as e: + print(f"Error: File could not be read. Details: {e}") + return [] +#Yasin Sinan Code Block +def add_task(tasks, tasks_file): + task_name = input("Enter the task name: ") + task_id = 1 + + for i in tasks: + if i["id"] : + task_id = i["id"] + 1 + + tasks.append({"id": task_id, "name": task_name, "status": "Pending"}) + + save_file(tasks_file, tasks) + + print("Newstask added successfully") + return True + + +def complete_task(tasks, task_id, tasks_file): + for i in tasks: + if i["id"] == task_id: + i["status"] = "completed" + + save_file(tasks_file, tasks) + + print("Task status is changed") + return True + + print("Task is not found") + return False + +#Kahraman Dal Code Block + +def delete_task(tasks, task_id, tasks_file): + + task_found = False + for task in tasks: + if task.get("id") == task_id: + task_found = True + task["status"] = "Deleted" + task["id"] = None + break + + if not task_found: + print("Task not found") + return False + + + for task in tasks: + if task["id"] != None and task["id"] > task_id: + task["id"] -= 1 + + save_file(tasks_file, tasks) + + print(f"Task with ID {task_id} has been successfully deleted.") + return True + +#Neslihan Code Block +# List Tasks +def list_tasks(tasks, status=["Pending", "Completed"]): + """ + Lists all tasks with their status in a grid format. + """ + filtered_tasks = [task for task in tasks if task["status"] in status] + + #1 Print grid header + print(f"\n{'ID':<10}{'Name':<60}{'Status':<15}") + + print("-" * 85) + #* 50: Tire karakterini 50 defa tekrarlayarak bir çizgi oluşturur. + + for task in filtered_tasks: + print(f"{str(task['id']):<10}{task['name']:<60}{task['status']:<15}") + +#print_grid(None, filtered_tasks) + task_count = len(filtered_tasks) + print(f"\n Total tasks listed: {task_count}") + + return task_count + + + +# Mustafa Gundogdu Code Block +if __name__ == "__main__": + tasks_file = "tasks.json" + tasks = load_file(tasks_file) + incorrectly_entered = False + exit_confirmation = True + main_menu = ["╔════════════════════════════════════════════════════════════╗ ", + "║ WELCOME TO TASK MANAGER ║ ", + "╠════════════════════════════════════════════════════════════╣ ", + "║ ║ ", + "║ (1) Add a new task ║ ", + "║ (2) Complete a task ║ ", + "║ (3) Delete a task ║ ", + "║ (4) List commpleted tasks ║ ", + "║ (5) List all tasks with their status ║ ", + "║ ║ ", + "╠════════════════════════════════════════════════════════════╣ ", + "║ (Q) Quit / Exit ║ ", + "╚════════════════════════════════════════════════════════════╝ " + ] + while exit_confirmation: + os.system("cls" if os.name == "nt" else "clear") + if tasks : + list_tasks(tasks,["Pending"]) + for i in main_menu: + print(i) + if incorrectly_entered: + print("Incorrect choice. Please try again.") + incorrectly_entered = False + + choice = input("Enter your choice: ") + if choice in ["q", "Q", "exit", "EXIT", "6"]: + exit_confirmation = False + elif choice == "1": + if not add_task(tasks, tasks_file): + print("Error: Task could not be added.") + input("Press Enter to continue...") + elif choice == "2": + task_id = int(input("Enter the task id: ")) + if not complete_task(tasks, task_id, tasks_file): + print("Error: Task could not be completed.") + input("Press Enter to continue...") + elif choice == "3": + task_id = int(input("Enter the task id: ")) + if not delete_task(tasks, task_id, tasks_file): + print("Error: Task could not be deleted.") + input("Press Enter to continue...") + elif choice == "4": + tasks_count = list_tasks(tasks, ["Completed"]) + input(f"{tasks_count} tasks completed. Press Enter to continue...") + elif choice == "5": + tasks_count = list_tasks(tasks,["Pending", "Completed", "Deleted"]) + input(f"All missions total {tasks_count}. Press Enter to continue...") + else: + incorrectly_entered = True + else: + print("Thank you for using the Task Manager. Goodbye!") + input("Press Enter to exit...") diff --git a/Week3.py b/Week3.py new file mode 100644 index 0000000..b67d3b0 --- /dev/null +++ b/Week3.py @@ -0,0 +1,232 @@ +""" +Question 1: Task Manager Application +Project Description: In this assignment, you will create a task manager application using the Python programming language. This application will allow users to add, complete, delete, and list their tasks. + +Requirements: +1- Tasks will be stored in a Python list and each task will be represented as a dictionary. Each task must have the following properties: + +Sequence Number (Automatically assigned) + +Task Name + +Status (Completed, Pending, or Deleted) + +2- Operations that the user can perform: + +Add a new task + +Complete a task + +Delete a task + +List completed tasks + +List all tasks with their status + +Exit + +3- Tasks should automatically receive a sequence number in the order they are added. + +4- New tasks should be saved in place of the numbers of deleted tasks. + +5- When listing tasks, they should be sorted by their sequence number. + +6- Appropriate feedback should be given to the user after each operation. For example, when a new task is added, they should see a message indicating that the task has been added. +""" +import os +import json +def create_grid(tasks, field_names, field_widths, grid_name): + """ + Creates a grid of tasks. + """ + start_first_row = "╔" + end_first_row = "╗" + start_middle_row = "╠" + end_middle_row = "╣" + start_last_row = "╚" + end_last_row = "╝" + separator_column = "║" + separator_row = "═" + separator_sub_row = "─" + separator_sub_column = "│" + grid = [] + grid_first_row = start_first_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_first_row + grid.append(grid_first_row) + grid_middle_row = separator_column+grid_name.center(sum(field_widths)+((len(field_widths)-1)*3)," ")+separator_column + grid.append(grid_middle_row) + grid_middle_row = start_middle_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_middle_row + grid.append(grid_middle_row) + field_counter = 0 + for field_name in field_names: + if field_counter == 0: + grid_middle_row = separator_column+field_name.center(field_widths[field_names.index(field_name)]," ") + if field_counter != len(field_widths)-1: + grid_middle_row += " "+separator_column+" " + else: + grid_middle_row += " "+separator_sub_column+" " + elif field_counter == len(field_widths)-1: + grid_middle_row += field_name.center(field_widths[field_counter]," ")+separator_column + else: + grid_middle_row += field_name.center(field_widths[field_names.index(field_name)]," ")+" "+separator_sub_column+" " + field_counter += 1 + grid.append(grid_middle_row) + grid_middle_row = separator_column+separator_sub_row*(sum(field_widths)+((len(field_widths)-1)*3))+separator_column + grid.append(grid_middle_row) + for task in tasks: + field_counter = 0 + for field_name in field_names: + if field_counter == 0: + grid_middle_row = separator_column + str(task["id"]).center(field_widths[field_counter]," ") + if field_counter == len(field_widths)-1: + grid_middle_row += " "+separator_column+" " + else: + grid_middle_row += " "+separator_sub_column+" " + elif field_counter == len(field_widths)-1: + grid_middle_row += task[field_name].center(field_widths[field_counter]," ")+separator_column + else: + grid_middle_row += task[field_name].center(field_widths[field_names.index(field_name)]," ")+" "+separator_sub_column+" " + field_counter += 1 + grid.append(grid_middle_row) + if tasks.index(task) != len(tasks)-1: + grid_middle_row = separator_column+separator_sub_row*(sum(field_widths)+((len(field_widths)-1)*3))+separator_column + grid.append(grid_middle_row) + grid_last_row = start_last_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_last_row + grid.append(grid_last_row) + return grid + +def save_file(file_path, data): + try: + with open(file_path, 'w', encoding='utf-8') as file: + json.dump(data, file, ensure_ascii=False, indent=4) + # print(f"Data was successfully written to file: {file_path}") + except Exception as e: + print(f"Error: Data could not be written to file. Details: {e}") +def load_file(file_path): + """ + Reads a list from a file in JSON format. + """ + if not os.path.exists(file_path): + save_file(file_path, []) + return [] + try: + with open(file_path, 'r', encoding='utf-8') as file: + return json.load(file) + except json.JSONDecodeError: + print("Error: JSON format is invalid.") + return [] + except Exception as e: + print(f"Error: File could not be read. Details: {e}") + return [] + +def add_task(tasks, tasks_file): + """ + Adds a new task to the task list. + """ + task_id = max(task["id"] for task in tasks) + 1 if tasks else 1 + task_name = input("Enter the task name: ") + task = { + "id": task_id, + "name": task_name, + "status": "Pending" + } + tasks.append(task) + save_file(tasks_file, tasks) + print(f"Task {task_name} added successfully.") + return True + +def complete_task(tasks, task_id, tasks_file): + """ + Completes the task with the given id. + """ + for task in tasks: + if task["id"] == task_id: + task["status"] = "Completed" + save_file(tasks_file, tasks) + print(f"Task {task["name"]} completed successfully.") + return True + return False + +def delete_task(tasks, task_id, tasks_file): + """ + Deletes the task with the given id. + """ + finding_task = False + for task in tasks: + if task["id"] != None: + if task["id"] == task_id: + task["id"] = None + task["status"] = "Deleted" + finding_task = True + elif task["id"] > task_id: + task["id"] -= 1 + if finding_task: + save_file(tasks_file, tasks) + else: + print("Task not found.") + return finding_task + +def list_tasks(tasks, statatus = ["Pending"]): + """ + Lists all tasks with their status. + """ + listed_tasks = [task for task in tasks if task["status"] in statatus] + grid = create_grid(listed_tasks, ["id", "name", "status"], [5, 50, 12], "Tasks List") + for row in grid: + print(row) + return len(listed_tasks) + + +if __name__ == "__main__": + tasks_file = "tasks.json" + tasks = load_file(tasks_file) + incorrectly_entered = False + exit_confirmation = True + main_menu = ["╔════════════════════════════════════════════════════════════╗ ", + "║ WELCOME TO TASK MANAGER ║ ", + "╠════════════════════════════════════════════════════════════╣ ", + "║ ║ ", + "║ (1) Add a new task ║ ", + "║ (2) Complete a task ║ ", + "║ (3) Delete a task ║ ", + "║ (4) List commpleted tasks ║ ", + "║ (5) List all tasks with their status ║ ", + "║ ║ ", + "╠════════════════════════════════════════════════════════════╣ ", + "║ (Q) Quit / Exit ║ ", + "╚════════════════════════════════════════════════════════════╝ " + ] + while exit_confirmation: + os.system("cls" if os.name == "nt" else "clear") + if tasks : + list_tasks(tasks) + for i in main_menu: + print(i) + if incorrectly_entered: + print("Incorrect choice. Please try again.") + incorrectly_entered = False + + choice = input("Enter your choice: ") + if choice in ["q", "Q", "exit", "EXIT", "6"]: + exit_confirmation = False + elif choice == "1": + if not add_task(tasks, tasks_file): + print("Error: Task could not be added.") + input("Press Enter to continue...") + elif choice == "2": + task_id = int(input("Enter the task id: ")) + if not complete_task(tasks, task_id, tasks_file): + print("Error: Task could not be completed.") + input("Press Enter to continue...") + elif choice == "3": + task_id = int(input("Enter the task id: ")) + if not delete_task(tasks, task_id, tasks_file): + print("Error: Task could not be deleted.") + input("Press Enter to continue...") + elif choice == "4": + tasks_count = list_tasks(tasks, ["Completed"]) + input(f"{tasks_count} tasks completed. Press Enter to continue...") + elif choice == "5": + tasks_count = list_tasks(tasks,["Pending", "Completed", "Deleted"]) + input(f"All missions total {tasks_count}. Press Enter to continue...") + else: + incorrectly_entered = True \ No newline at end of file diff --git a/hackerrank.py b/hackerrank.py new file mode 100644 index 0000000..c98d8ef --- /dev/null +++ b/hackerrank.py @@ -0,0 +1,27 @@ +# # https://www.hackerrank.com/challenges/find-digits/problem + +def findDigits(n): + # Write your code here + str_n = str(n) + dividers = 0 + for i in str_n: + if i != "0" and n % int(i) == 0: + dividers +=1 + return dividers + +"""---------------------------------------------""" +# # https://www.hackerrank.com/challenges/capitalize/problem +def solve(s): + return_s = "" + uppercase_ = True + for i in s: + if (i != " ") and not i.isnumeric() and uppercase_ : + return_s += i.upper() + uppercase_ = False + elif i == " ": + return_s += i + uppercase_ = True + else: + return_s += i + uppercase_ = False + return return_s \ No newline at end of file