Skip to content

Comparing Python and JavaScript for Top Algorithms + OOP

Notifications You must be signed in to change notification settings

dfrho/python-and-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

For anyone experienced in both JavaScript and Python, here's a list of algorithms providing some mental dexterity on the differences by summarizing the learning with both implementations, along with The Task explanations:

Learning Summary: Python and JavaScript Implementations

This document summarizes several key programming concepts in Python and JavaScript, including Fibonacci numbers, list comprehension, dictionary manipulation, error handling, prime number finding, string manipulation, file processing, and object-oriented programming.

1. Fibonacci Numbers

The Task

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. We can implement this sequence using recursion, where the function calls itself to solve smaller subproblems.

Python Implementation

def calculate_fibonacci(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)

# Example usage
n = 10
print(f"The {n}th Fibonacci number is: {calculate_fibonacci(n)}")


### Generator Python Implementation
#### Benefits of Using a GeneratorMemory Efficiency: Generators compute the values on-the-fly and do not store the entire sequence in memory.
	•Lazy Evaluation: Generators produce items only when requested, making them suitable for handling large datasets or infinite sequences.
	•Simple and Clean Code: Generators provide a simple syntax for iteration, making code easy to read and maintain.

```python
def fibonacci_generator(max_value):
    """A generator function for Fibonacci numbers up to max_value."""
    a, b = 0, 1
    while a <= max_value:
        yield a
        a, b = b, a + b

# Example usage:
max_value = 100  # Define the maximum value for the Fibonacci sequence
fib_sequence = fibonacci_generator(max_value)

# Print the Fibonacci sequence up to the max_value
for number in fib_sequence:
    print(number)

JavaScript Implementation

function calculateFibonacci(n) {
    if (n <= 0) {
        return 0;
    } else if (n === 1) {
        return 1;
    } else {
        return calculateFibonacci(n - 1) + calculateFibonacci(n - 2);
    }
}

// Example usage
const n = 10;
console.log(`The ${n}th Fibonacci number is: ${calculateFibonacci(n)}`);

2. List Comprehension

The Task

List comprehension in Python is a concise way to create lists by iterating over iterables and optionally including conditions. In JavaScript, we use array methods or loops to achieve similar results.

Python Implementation

def question_1():
    result = [x for x in range(1, 21) if x % 2 == 0 or x % 3 == 0]
    return result

print(question_1())
# Output: [2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20]

JavaScript Implementation

function findNumbers() {
    const result = [];
    for (let x = 1; x <= 20; x++) {
        if (x % 2 === 0 || x % 3 === 0) {
            result.push(x);
        }
    }
    return result;
}

console.log(findNumbers());
// Output: [2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20]

3. Working with Dictionaries / Objects

The Task

In Python, dictionaries are used to store key-value pairs, similar to objects in JavaScript. We can manipulate dictionaries to sort or access data efficiently.

Python Implementation

def question_2(prices):
    sorted_price_list = sorted(prices.items())
    return sorted_price_list

prices = {'apple': 0.40, 'banana': 0.50, 'kiwi': 1.25}
print(question_2(prices))
# Output: [('apple', 0.4), ('banana', 0.5), ('kiwi', 1.25)]

JavaScript Implementation

function sortPrices(prices) {
    const priceList = Object.entries(prices);
    priceList.sort((a, b) => a[0].localeCompare(b[0]));
    return priceList;
}

const prices = {'apple': 0.40, 'banana': 0.50, 'kiwi': 1.25};
console.log(sortPrices(prices));
// Output: [['apple', 0.4], ['banana', 0.5], ['kiwi', 1.25]]

4. Error Handling

The Task

Error handling allows us to manage exceptions and unexpected situations in our code, ensuring the program runs smoothly. Python uses try-except blocks, while JavaScript uses try-catch.

Python Implementation

def divide_numbers(numerator, denominator):
    try:
        return numerator / denominator
    except ZeroDivisionError:
        return -1

print(divide_numbers(10, 2))  # Should output 5.0
print(divide_numbers(10, 0))  # Should output -1

JavaScript Implementation

function divideNumbers(numerator, denominator) {
    try {
        if (denominator === 0) throw new Error("Divide by zero");
        return numerator / denominator;
    } catch (error) {
        return -1;
    }
}

console.log(divideNumbers(10, 2));  // Should output 5.0
console.log(divideNumbers(10, 0));  // Should output -1

5. Finding Prime Numbers

The Task

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. We can use loops to determine prime numbers by checking for divisibility.

Python Implementation

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def find_primes(start, end):
    primes = []
    for num in range(start, end + 1):
        if is_prime(num):
            primes.append(num)
    return primes

print(find_primes(10, 50))
# Output: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

JavaScript Implementation

function isPrime(n) {
    if (n <= 1) return false;
    for (let i = 2; i <= Math.sqrt(n); i++) {
        if (n % i === 0) return false;
    }
    return true;
}

function findPrimes(start, end) {
    const primes = [];
    for (let num = start; num <= end; num++) {
        if (isPrime(num)) primes.push(num);
    }
    return primes;
}

console.log(findPrimes(10, 50));
// Output: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

6. String Manipulation

The Task

String manipulation involves processing and analyzing strings. Checking if a string is a palindrome involves determining if it reads the same forward and backward, ignoring case and non-alphanumeric characters.

Python Implementation

def is_palindrome(s):
    s = ''.join(char for char in s if char.isalnum()).lower()
    return s == s[::-1]

print(is_palindrome("A man, a plan, a canal, Panama"))  # Should return True
print(is_palindrome("Hello, World!"))  # Should return False

JavaScript Implementation

function isPalindrome(s) {
    s = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
    return s === s.split('').reverse().join('');
}

console.log(isPalindrome("A man, a plan, a canal, Panama"));  // Should return true
console.log(isPalindrome("Hello, World!"));  // Should return false

7. File Processing

The Task

File processing involves reading from and writing to files. In Python, we use the built-in open function to manage files, while JavaScript requires Node.js with the fs module for similar functionality.

Python Implementation

def process_file(input_file, output_file):
    with open(input_file, 'r') as infile:
        text = infile.read()
    words = text.split()
    word_counts = {}
    for word in words:
        word_counts[word] = word_counts.get(word, 0) + 1
    sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
    with open(output_file, 'w') as outfile:
        for word, count in sorted_word_counts:
            outfile.write(f"{word}: {count}\n")

# Example usage
# process_file('input.txt', 'output.txt')

JavaScript Implementation

const fs = require('fs');

function processFile(inputFile, outputFile) {
    const text = fs.readFileSync(inputFile, 'utf8');
    const words = text.split(/\s+/);
    const wordCounts = {};

    words.forEach(word => {
        wordCounts[word] = (wordCounts[word] || 0) + 1;
    });

    const sortedWordCounts = Object.entries(wordCounts).sort((a, b) => b[1] - a[1]);

    const outputText = sortedWordCounts.map(([word, count]) => `${word}: ${count}`).join('\n');
    fs.writeFileSync(outputFile, outputText);
}

// Example usage
// processFile('input.txt', 'output.txt');

Certainly! Let's continue with the The Task and implementation of the Object-Oriented Programming (OOP) task, starting with the Python implementation:


8. Object-Oriented Programming

The Task

Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to design and structure software. Objects are instances of classes, which can contain attributes (data) and methods (functions) that operate on the data. OOP is a powerful way to model real-world systems and their interactions in a semantically intuitive way.

In this task, we'll create a simple Library Management System using OOP principles with classes for Book, Member, and Library.

Python Implementation

Book Class

  • Attributes: title, author, isbn
  • Methods: A string representation to display the book's information.
class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn

    def __str__(self):
        return f"{self.title} by {self.author}"

Member Class

  • Attributes: name, member_id, books_checked_out
  • Methods:
    • check_out_book: Adds a book to the member’s list of checked-out books.
    • return_book: Removes a book from the list of checked-out books.
    • A string representation to display member details.
class Member:
    def __init__(self, name, member_id):
        self.name = name
        self.member_id = member_id
        self.books_checked_out = []

    def check_out_book(self, book):
        self.books_checked_out.append(book)

    def return_book(self, book):
        if book in self.books_checked_out:
            self.books_checked_out.remove(book)

    def __str__(self):
        books = ', '.join(str(book) for book in self.books_checked_out)
        return f"{self.name} (ID: {self.member_id}) - {books if books else 'No books checked out'}"

Library Class

  • Attributes: name, books, members
  • Methods:
    • add_book: Adds a new book to the library.
    • add_member: Registers a new member in the library.
    • check_out_book: Allows a member to check out a book if it is available.
    • return_book: Allows a member to return a checked-out book.
    • get_member_books: Displays all books checked out by a member.
    • A string representation to display library details.
class Library:
    def __init__(self, name):
        self.name = name
        self.books = []
        self.members = []

    def add_book(self, book):
        self.books.append(book)

    def add_member(self, member):
        self.members.append(member)

    def check_out_book(self, member_id, isbn):
        member = next((m for m in self.members if m.member_id == member_id), None)
        book = next((b for b in self.books if b.isbn == isbn), None)
        if member and book and book not in member.books_checked_out:
            member.check_out_book(book)
            self.books.remove(book)

    def return_book(self, member_id, isbn):
        member = next((m for m in self.members if m.member_id == member_id), None)
        book = next((b for b in member.books_checked_out if b.isbn == isbn), None)
        if member and book:
            member.return_book(book)
            self.books.append(book)

    def get_member_books(self, member_id):
        member = next((m for m in self.members if m.member_id == member_id), None)
        if member:
            print(f"{member.name} has checked out the following books:")
            for book in member.books_checked_out:
                print(book)
        else:
            print(f"No member found with ID: {member_id}")

    def __str__(self):
        return f"{self.name} - {len(self.books)} books, {len(self.members)} members"

JavaScript Implementation

Book Class

  • Attributes: title, author, isbn
  • Methods: A method to display the book's information.
class Book {
    constructor(title, author, isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }

    toString() {
        return `${this.title} by ${this.author}`;
    }
}

Member Class

  • Attributes: name, memberId, booksCheckedOut
  • Methods:
    • checkOutBook: Adds a book to the member’s list of checked-out books.
    • returnBook: Removes a book from the list of checked-out books.
    • A method to display member details.
class Member {
    constructor(name, memberId) {
        this.name = name;
        this.memberId = memberId;
        this.booksCheckedOut = [];
    }

    checkOutBook(book) {
        this.booksCheckedOut.push(book);
    }

    returnBook(book) {
        const index = this.booksCheckedOut.indexOf(book);
        if (index !== -1) {
            this.booksCheckedOut.splice(index, 1);
        }
    }

    toString() {
        const books = this.booksCheckedOut.map(book => book.toString()).join(', ');
        return `${this.name} (ID: ${this.memberId}) - ${books || 'No books checked out'}`;
    }
}

Library Class

  • Attributes: name, books, members
  • Methods:
    • addBook: Adds a new book to the library.
    • addMember: Registers a new member in the library.
    • checkOutBook: Allows a member to check out a book if it is available.
    • returnBook: Allows a member to return a checked-out book.
    • getMemberBooks: Displays all books checked out by a member.
    • A method to display library details.
class Library {
    constructor(name) {
        this.name = name;
        this.books = [];
        this.members = [];
    }

    addBook(book) {
        this.books.push(book);
    }

    addMember(member) {
        this.members.push(member);
    }

    checkOutBook(memberId, isbn) {
        const member = this.members.find(m => m.memberId === memberId);
        const bookIndex = this.books.findIndex(b => b.isbn === isbn);
        if (member && bookIndex !== -1) {
            member.checkOutBook(this.books[bookIndex]);
            this.books.splice(bookIndex, 1);
        }
    }

    returnBook(memberId, isbn) {
        const member = this.members.find(m => m.memberId === memberId);
        if (member) {
            const book = member.booksCheckedOut.find(b => b.isbn === isbn);
            if (book) {
                member.returnBook(book);
                this.books.push(book);
            }
        }
    }

    getMemberBooks(memberId) {
        const member = this.members.find(m => m.memberId === memberId);
        if (member) {
            console.log(`${member.name} has checked out the following books:`);
            member.booksCheckedOut.forEach(book => console.log(book.toString()));
        } else {
            console.log(`No member found with ID: ${memberId}`);
        }
    }

    toString() {
        return `${this.name} - ${this.books.length} books, ${this.members.length} members`;
    }
}

Example Usage

Python

def challenge_question():
    # Create a library
    my_library = Library("City Library")

    # Add books to the library
    book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "1234567890")
    book2 = Book("To Kill a Mockingbird", "Harper Lee", "1234567891")
    book3 = Book("1984", "George Orwell", "1234567892")

    my_library.add_book(book1)
    my_library.add_book(book2)
    my_library.add_book(book3)

    # Add members to the library
    member1 = Member("Alice", 1)
    member2 = Member("Bob", 2)

    my_library.add_member(member1)
    my_library.add_member(member2)

    # Check out and return books
    my_library.check_out_book(1, "1234567890")
    my_library.check_out_book(2, "1234567891")

    print(member1)
    print(member2)

    my_library.return_book(1, "1234567890")

    print(member1)
    print(my_library)

challenge_question()

JavaScript

function challengeQuestion() {
    // Create a library
    const myLibrary = new Library("City Library");

    // Add books to the library
    const book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "1234567890");
    const book2 = new Book("To Kill a Mockingbird", "Harper Lee", "1234567891");
    const book3 = new Book("1984", "George Orwell", "1234567892");

    myLibrary.addBook(book1);
    myLibrary.addBook(book2);
    myLibrary.addBook(book3);

    // Add members to the library
    const member1 = new Member("Alice", 1);
    const member2 = new Member("Bob", 2);

    myLibrary.addMember(member1);
    myLibrary.addMember(member2);

About

Comparing Python and JavaScript for Top Algorithms + OOP

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published