Skip to main content

Recent Posts

class based task manager

Jun 1, 2023CodeCatch

0 likes • 4 views

interface Task {
id: number;
title: string;
completed: boolean;
}
class TaskManager {
private tasks: Task[] = [];
constructor() {}
addTask(title: string) {
const taskId = this.tasks.length + 1;
const task: Task = {
id: taskId,
title,
completed: false,
};
this.tasks.push(task);
}
markTaskAsComplete(taskId: number) {
const task = this.tasks.find((task) => task.id === taskId);
if (task) {
task.completed = true;
}
}
markTaskAsIncomplete(taskId: number) {
const task = this.tasks.find((task) => task.id === taskId);
if (task) {
task.completed = false;
}
}
listTasks() {
console.log('Tasks:');
this.tasks.forEach((task) => {
console.log(`${task.id}. [${task.completed ? 'X' : ' '}] ${task.title}`);
});
}
}
// Example usage:
const taskManager = new TaskManager();
taskManager.addTask('Buy groceries');
taskManager.addTask('Pay bills');
taskManager.addTask('Clean the house');
taskManager.listTasks();
// Output:
// Tasks:
// 1. [ ] Buy groceries
// 2. [ ] Pay bills
// 3. [ ] Clean the house

convert bytes to a string

Jun 1, 2023CodeCatch

0 likes • 2 views

bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

return multiple values from a function

Jun 1, 2023CodeCatch

0 likes • 2 views

def calculate_values():
value1 = 10
value2 = 20
return value1, value2
result1, result2 = calculate_values()
print("Result 1:", result1)
print("Result 2:", result2)

read file contents into a list

Jun 1, 2023CodeCatch

0 likes • 0 views

filename = "data.txt"
with open(filename, "r") as file:
file_contents = file.readlines()
file_contents = [line.strip() for line in file_contents]
print("File contents:")
for line in file_contents:
print(line)

print colored text to IDE terminal

Jun 1, 2023CodeCatch

0 likes • 2 views

from colorama import init, Fore
# Initialize colorama
init()
print(Fore.RED + "This text is in red color.")
print(Fore.GREEN + "This text is in green color.")
print(Fore.BLUE + "This text is in blue color.")
# Reset colorama
print(Fore.RESET + "This text is back to the default color.")

Append to a file

Jun 1, 2023CodeCatch

0 likes • 0 views

filename = "data.txt"
data = "Hello, World!"
with open(filename, "a") as file:
file.write(data)

Post Statistics

Posts

No Posts Found

It looks like CodeCatch has no public posts

Likes

Please Log In

You must be authenticated to view a user's likes

Shared

Please Log In

You must be authenticated to view a user's shared posts

Profile Privacy

Multi-Factor Authentication

Multi-Factor Authentication (MFA) is an authentication method that requires you to provide two or more verification factors to gain access to your account. In addition to username and password, MFA requires you to verify your email on every login, which decreases the likelihood of someone stealing your account.

Change Password

Forgot Password?

Identity Color

Changes the color of your profile icon and cursor highlight in the live code editor. You and other users will be able to view this change.

Delete Account

Deleting your account is permanent. All data associated with your account will be lost.