Sum of Odd Cubed Numbers in Python

The challenge Find the sum of the odd numbers within an array, after cubing the initial integers. The function should return None if any of the values aren’t numbers. Note: Booleans should not be considered as numbers. The solution in Python code Option 1: def cube_odd(arr): if any(type(x) is not int for x in arr): return None return sum(x ** 3 for x in arr if x % 2 != 0) Option 2:...

September 4, 2021 · 1 min · 199 words · Andrew

Halving Sum in Python

The challenge Given a positive integer n, calculate the following sum: n + n/2 + n/4 + n/8 + ... All elements of the sum are the results of integer division. Example 25 => 25 + 12 + 6 + 3 + 1 = 47 The solution in Python code Option 1: def halving_sum(n): total = [n] while n>=1: n = int(n/2) total.append(n) return sum(total) Option 2: def halving_sum(n): s=0 while n: s+=n ; n>>=1 return s Option 3:...

September 3, 2021 · 1 min · 104 words · Andrew

Convert a LinkedList to a String in Python

The challenge Preloaded for you is a class, struct, or derived data type Node (depending on the language) used to construct linked lists in this challenge: class Node(): def __init__(self, data, next = None): self.data = data self.next = next Create a function stringify which accepts an argument list/$list and returns a string representation of the list. The string representation of the list starts with the value of the current Node, specified by its data/$data/Data property, followed by a whitespace character, an arrow, and another whitespace character (" -> "), followed by the rest of the list....

September 2, 2021 · 2 min · 318 words · Andrew

Most Digits from List in Python

The challenge Find the number with the most digits. If two numbers in the argument array have the same number of digits, return the first one in the array. The solution in Python code Option 1: def find_longest(xs): return max(xs, key=lambda x: len(str(x))) Option 2: def find_longest(arr): arr.sort(reverse=True) return arr[0] Option 3: def find_longest(arr): max_lenght = 0 max_index = 0 for cur_num in arr: lenght = len(str(cur_num)) if lenght > max_lenght: max_lenght = lenght max_index = arr....

September 1, 2021 · 1 min · 159 words · Andrew

Check if all Values in Array are Smaller in Python

The challenge You will be given an array and a limit value. You must check that all values in the array are below or equal to the limit value. If they are, return true. Else, return false. You can assume all values in the array are numbers. The solution in Python code Option 1: def small_enough(array, limit): return True if max(array)<=limit else False Option 2: def small_enough(array, limit): return max(array)<=limit Option 3:...

August 31, 2021 · 1 min · 189 words · Andrew

Maximum Product from List of Integers in Python

The challenge Given an array of integers, Find the maximum product obtained from multiplying 2 adjacent numbers in the array. Notes Array/list size is at least 2. Array/list numbers could be a mixture of positives, negatives also zeroes . Input >Output Examples adjacentElementsProduct([1, 2, 3]); ==> return 6 Explanation: The maximum product is obtained from multiplying 2 * 3 = 6, and they’re adjacent numbers in the array. adjacentElementsProduct([9, 5, 10, 2, 24, -1, -48]); ==> return 50 Explanation: Max product obtained from multiplying 5 * 10 = 50&nbsp;....

August 30, 2021 · 2 min · 282 words · Andrew

Sum of Array Singles in Python

The challenge You are given an array of numbers in which two numbers occur once and the rest occur only twice. Your task is to return the sum of the numbers that occur only once. For example, repeats([4,5,7,5,4,8]) = 15 because only the numbers 7 and 8 occur once, and their sum is 15. Every other number occurs twice. The solution in Python code Option 1: def repeats(arr): count = [] for i in arr: if i not in count: count....

August 29, 2021 · 1 min · 140 words · Andrew

Flatten and Sort an Array in Python

The challenge Given a two-dimensional array of integers, return the flattened version of the array with all the integers in the sorted (ascending) order. Example: Given [[3, 2, 1], [4, 6, 5], [], [9, 7, 8]], your function should return [1, 2, 3, 4, 5, 6, 7, 8, 9]. The solution in Python code Option 1: def flatten_and_sort(array): a = [] for b in array: for c in b: a.append(c) return sorted(a) Option 2:...

August 28, 2021 · 1 min · 154 words · Andrew

Playing the Alphabet War in Python

The challenge Introduction There is a war and nobody knows – the alphabet war! There are two groups of hostile letters. The tension between left side letters and right side letters was too high and the war began. Task Write a function that accepts fight string consists of only small letters and return who wins the fight. When the left side wins return Left side wins!, when the right side wins return Right side wins!...

August 27, 2021 · 2 min · 335 words · Andrew

Sum of numbers from 0 to N in Python

The challenge We want to generate a function that computes the series starting from 0 and ending until the given number. Example: Input: > 6 Output: 0+1+2+3+4+5+6 = 21 Input: > -15 Output: -15<0 Input: > 0 Output: 0=0 The solution in Python code Option 1: def show_sequence(n): if n == 0: return "0=0" elif n < 0: return str(n) + "<0" else: counter = sum(range(n+1)) return '+'.join(map(str, range(n+1))) + " = " + str(counter) Option 2:...

August 26, 2021 · 2 min · 234 words · Andrew