Challenges
Explore amazing python coding challenges from beginner to expert
Showing 1-12 of 174 results
Valid Palindrome
A palindrome is a word that reads the same backward as forward. Examples include "mom", "racecar", and "level". Today, you're going to write a function that checks if a given string is a palindrome! Here’s how your task goes: Write a function `is_palindrome` that takes a single string parameter and returns `True` if the string is a palindrome, and `False` otherwise. Don't worry about spaces or punctuation! Just consider the letters. Also, consider that the input string can mix both upper and lower case letters, so "Level" should be considered a palindrome. Examples ```python is_palindrome("racecar") # Returns: True is_palindrome("palindrome") # Returns: False is_palindrome("A man a plan a canal Panama") # Returns: True is_palindrome("Was it a car or a cat I saw") # Returns: True is_palindrome("No lemon no melon") # Returns: True ``` Your goal is to create a function that successfully identifies palindromes from any given string that may include spaces and mixed case letters.
Inorder Successor in BST
In this challenge, we'll learn about Binary Search Trees and find the Inorder Successor of a given node. Imagine climbing a tree where each node has a left and right child. If you stand on one node, the Inorder Successor is the node you would visit next in an in-order traversal (left-root-right). Let's play with a Binary Search Tree (BST) using numbers: When you traverse a BST in-order, you visit the nodes in increasing order. For any node, the inorder successor is the smallest node larger than itself located to the right of it. For example: ``` Input tree: 5 / \ 3 8 / \ \ 2 4 9 If the target node is `3`, the inorder successor is `4`. If the target node is `5`, the inorder successor is `8`. ``` Your task is to implement a function `inorder_successor_in_bst` that returns the value of the inorder successor given the root of a BST and a target node. If the target node has no successor (e.g., it's the largest element), return `None`. Let's look at some examples: ```python # Create a simple Binary Search Tree for testing root = TreeNode(5) node3 = TreeNode(3) node4 = TreeNode(4) node8 = TreeNode(8) node2 = TreeNode(2) node9 = TreeNode(9) root.left = node3 root.right = node8 node3.left = node2 node3.right = node4 node8.right = node9 print(inorder_successor_in_bst(root, node3)) # Output: 4 print(inorder_successor_in_bst(root, node5)) # Output: 8 print(inorder_successor_in_bst(root, node9)) # Output: None ``` How the tests work Each test gives the tree as nested `val`, `left` and `right` entries, plus the value of the target node. The tree is built for you, and your function receives the real target node.
Find Median from Data Stream
Find Median from Data Stream In this challenge, your task is to write a Python function that finds the median from a stream of numbers. The median is the value separating the higher half from the lower half of a data sample. If the number of data points is odd, the median is the middle number. If the number of data points is even, the median is the average of the two middle numbers. Let's imagine that you're building a real-time statistics app for a school project that calculates the median of scores as they are entered. Your code should be efficient enough to handle lots of data without slowing down the app. Your function should keep a running list of numbers and be able to return the median at any point. #### Examples ```python find_median_from_data_stream([2, 3, 4, 5, 6]) # Returns: 4.0 find_median_from_data_stream([2, 3, 4, 5]) # Returns: 3.5 find_median_from_data_stream([12, 4, 5, 3, 8]) # Returns: 5.0 find_median_from_data_stream([7, 12, 3, 1, 6, 5]) # Returns: 5.5 ``` Fun Twist! Imagine you're the coder for a party, and your friends want to know the median of their favorite numbers at any time. Help them find the right value quickly!
Find Peak Element
In this fun coding challenge, we'll traverse through a list of numbers to find a "peak" element. A peak element is one that is **strictly greater** than its neighbors. Your task is to identify and return the index of any one peak element in a list of numbers. For simplicity, let's assume that the numbers are unique, and the list is never empty. A peak element can be at any position in the list. Example: ```python find_peak_element([1, 3, 20, 4, 1, 0]) # Returns 2 as '20' is a peak element because it is greater than '3' and '4'. find_peak_element([10, 7, 8, 20, 15]) # Returns 3 as '20' is a peak because it is greater than '8' and '15'. ``` In the case where the peak is at the beginning or end of the list: ```python find_peak_element([5, 3, 2, 1]) # Returns 0 since '5' is a peak because there are no elements before it. find_peak_element([1, 2, 3, 6]) # Returns 3 since '6' is a peak because there are no elements after it. ``` Help find the peaks and practice coding with this interesting challenge!
Largest of Three
In this challenge, you will write a Python function to find the largest number among three given numbers. Imagine you're a wizard who needs to compare the sizes of three magical stones, and you want to determine which stone is the largest. Your task is to implement a function `largest_of_three`, which takes three numbers as arguments and returns the largest of them. Here's how it should work: ```python largest_of_three(5, 10, 3) # This should return 10 largest_of_three(25, 7, 25) # This should return 25 largest_of_three(0, -10, -20) # This should return 0 ``` Let's make sure your magical powers are up to the task! Be precise and find the largest number among the three!
Convert Temperature
Convert Temperature Challenge Welcome to the Temperature Converter! In this challenge, your mission is to write a Python function that converts a temperature from Celsius to Fahrenheit. It's like having a little magical thermometer in your pocket, ready to change weather forecasts just for fun! 🌡️🎩 How does the conversion work? The formula to convert from Celsius to Fahrenheit is: \( F = C \times \frac{9}{5} + 32 \) Where: - **C** is the temperature in Celsius - **F** is the temperature in Fahrenheit Task Write a function called `convert_temperature` that takes one argument: - `celsius` (a float) representing the temperature in Celsius. The function should return the temperature in Fahrenheit as a float, rounded to one decimal place. Example Here's what your function should look like when it's executed: ```python convert_temperature(0) # Returns 32.0 convert_temperature(100) # Returns 212.0 convert_temperature(-40) # Returns -40.0 convert_temperature(37) # Returns 98.6 ``` Isn't it fun to see how temperatures change through conversions? 🌍✨ Now, give it a try and show your coding magic! 🎉
Permutations of Array
Description Hello, young coder! Today, you have a magical task to solve. We want to generate all possible **permutations** of a list of items. Imagine you have a set of colorful blocks on your table and you want to see all the different ways you can arrange these blocks! This is what permutations are all about. A permutation of an array is a different way to arrange all the elements in that array. For example, if you have an array with the elements `[1, 2, 3]`, some permutations would be `[1, 2, 3]`, `[1, 3, 2]`, `[2, 1, 3]`, etc. Your mission is to write a function that, given a list of items, returns a list of all possible permutations of the array. Examples Here's how you should implement the function: ```python permutations_of_array([1, 2, 3]) ``` Should return: ```python [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] ``` Another example: ```python permutations_of_array(["a", "b"]) ``` Should return: ```python [["a", "b"], ["b", "a"]] ``` Note: The order of permutations does not have to match exactly, but all possible permutations should be present.
Candy Distribution
Candy Distribution Challenge It's candy time! You've got a bag of candies and a bunch of friends. You need to distribute the candies fairly among your friends, and if there are any leftover candies, you have to decide what to do with them! Write a function `distribute_candies` that calculates how many candies each friend gets and how many are left over (if any). You will receive two numbers as input: 1. `candies` - The total number of candies. 2. `friends` - The number of friends you want to share the candies with. Your task is to return a tuple where the first element is the number of candies each friend gets, and the second element is the number of leftover candies. #### Examples: ```python # Example 1 candies = 23 friends = 4 output: (5, 3) # Each friend gets 5 candies, and there are 3 candies left over. # Example 2 candies = 10 friends = 2 output: (5, 0) # Each friend gets 5 candies, and no candies are left over. # Example 3 candies = 7 friends = 3 output: (2, 1) # Each friend gets 2 candies, and there is 1 candy left over. ``` Have fun sharing candies and happy coding!
Longest Univalue Path
Welcome to an exciting coding challenge where you will explore a mystical forest of trees! Each tree in this forest has nodes, and these nodes love to carry values. Some trees are mysterious—parts of their branches have repeated values, forming a path of the same number! Your mission, should you choose to accept it, is to discover the longest path in a tree where every node along the path has the same value. Remember, the path need not go through the root of the tree and can be any connected branches with the same value. **Your Task**: - You will be given a nested dictionary representing a binary tree. - Implement the function `longest_univalue_path(tree_dict)` that takes the nested dictionary and returns the length of the longest path where each node has the same value. - You need to build the tree from the nested dictionary within your function. **Example Tree Representations**: 1. For the tree: ``` 5 / \ 4 5 / \ \ 1 4 5 ``` The dictionary representation is: ```python { 'value': 5, 'left': { 'value': 4, 'left': {'value': 1}, 'right': {'value': 4} }, 'right': { 'value': 5, 'right': {'value': 5} } } ``` - The longest univalue path is `5 -> 5 -> 5`, so the function should return `2`. 2. For the tree: ``` 1 / \ 4 5 / \ \ 4 4 5 ``` The dictionary representation is: ```python { 'value': 1, 'left': { 'value': 4, 'left': {'value': 4}, 'right': {'value': 4} }, 'right': { 'value': 5, 'right': {'value': 5} } } ``` - The longest univalue path is `4 -> 4 -> 4`, so the function should return `2`.
Reverse a String
Reverse a String Challenge Welcome to the fun and exciting world of strings! In this challenge, you'll create a function that flips a string inside out, like putting your shirt on backwards but with words! This is a great way to practice working with strings and improve your problem-solving skills. Imagine you have a long word like "strawberry" but you want to know what it looks like backwards! Wouldn't it be cool if you could have a program that does it for you? That's exactly what you're going to create. Your mission is to write a function called `reverse_string` that takes a single word as input and returns that word spelled backwards. Here's what you'll do: - Take a word as a string. - Reverse the word. - Return the reversed word. #### Examples: ```python reverse_string("hello") # returns "olleh" reverse_string("world") # returns "dlrow" reverse_string("!abc123") # returns "321cba!" ``` Think of it like a magic mirror where left becomes right and right becomes left. Let's see if you can solve this string puzzle!
Find the Smallest Missing Positive Integer
Welcome, young coder! 🚀 In this challenge, we're going to embark on a magical journey to find a lost number! But not just any number... We're looking for the *smallest missing positive integer* from a given list of numbers. Let's help this number find its way back! Here's what you need to do: Write a function `find_smallest_missing_positive_integer` that takes a list of integers and returns the smallest positive integer that is not present in the list. Positive integers are numbers like 1, 2, 3, and so on. Examples: Imagine the list is `[1, 2, 0, 4, 5]`: ```python find_smallest_missing_positive_integer([1, 2, 0, 4, 5]) ``` The function should return `3` because 3 is the smallest positive number not in the list. For the list `[3, 4, -1, 1]`: ```python find_smallest_missing_positive_integer([3, 4, -1, 1]) ``` The result should be `2` since 2 is the smallest positive number not present. One last example, with a full continuous list `[1, 2, 3, 4, 5]`: ```python find_smallest_missing_positive_integer([1, 2, 3, 4, 5]) ``` It should return `6` because all other positive numbers before 6 are present. Let's save that missing number! 🧙♂️ Remember, the list can contain negative numbers and zeros, but we're only interested in finding the smallest missing positive integer.
Find Common Elements in Two Lists
In this fun challenge, you will learn to find common elements between two lists. Let's say we have two lists, and we want to find all the items that are present in both lists. Your task is to write a Python function, **find_common_elements** that takes two lists as inputs and returns a new list containing all the items that appear in both lists. The order of elements in the output list can be in the order they appear in the first list, and there should be no duplicates in the output list. For example, if the input lists are: ```python list1 = ['apple', 'banana', 'cherry', 'date'] list2 = ['banana', 'date', 'fig', 'grape'] ``` Then, the function should return: ```python ['banana', 'date'] ``` Another example: ```python list1 = [1, 2, 3, 4, 5] list2 = [0, 2, 4, 6, 8] ``` The function will return: ```python [2, 4] ``` Ready to make your lists talk to each other? Let's get started!
Showing 1-12 of 174 results