Skip to main content

CodeCatch

User since Oct 15, 2022
156 Posts
Python
JavaScript
Java
C
PHP
Perl
TypeScript
C++
CSS
HTML
MySQL
Shell
C#
PostgreSQL
Plaintext
SCSS
Swift

Recent Posts

class based task manager

0 likes • Jun 1, 2023 • 2 views
TypeScript
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

0 likes • Jun 1, 2023 • 0 views
Python
bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

return multiple values from a function

0 likes • Jun 1, 2023 • 0 views
Python
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

0 likes • Jun 1, 2023 • 0 views
Python
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

0 likes • Jun 1, 2023 • 0 views
Python
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

0 likes • Jun 1, 2023 • 0 views
Python
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 hasn't uploaded a post yet

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.