chore: import upstream snapshot with attribution
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 6814d8e1516e86b171929de4
|
||||
title: "Challenge 1: Vowel Balance"
|
||||
challengeType: 29
|
||||
dashedName: challenge-1
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, determine whether the number of vowels in the first half of the string is equal to the number of vowels in the second half.
|
||||
|
||||
- The string can contain any characters.
|
||||
- The letters `a`, `e`, `i`, `o`, and `u`, in either uppercase or lowercase, are considered vowels.
|
||||
- If there's an odd number of characters in the string, ignore the center character.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_balanced("racecar")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("racecar"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced("Lorem Ipsum")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("Lorem Ipsum"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced("Kitty Ipsum")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("Kitty Ipsum"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced("string")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("string"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced(" ")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced(" "), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced("abcdefghijklmnopqrstuvwxyz")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("abcdefghijklmnopqrstuvwxyz"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_balanced("123A#b!E&*456-o.U")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_balanced("123A#b!E&*456-o.U"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_balanced(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_balanced(s):
|
||||
vowels = set("aeiouAEIOU")
|
||||
n = len(s)
|
||||
half = n // 2
|
||||
|
||||
first_half = s[:half]
|
||||
second_half = s[-half:]
|
||||
|
||||
def count_vowels(sub):
|
||||
return sum(1 for char in sub if char in vowels)
|
||||
|
||||
return count_vowels(first_half) == count_vowels(second_half)
|
||||
```
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
---
|
||||
id: 681cb05adab50c87ddb2e513
|
||||
title: "Challenge 2: Base Check"
|
||||
challengeType: 29
|
||||
dashedName: challenge-2
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a number, and an integer base from 2 to 36, determine whether the number is valid in that base.
|
||||
|
||||
- The string may contain integers, and uppercase or lowercase characters.
|
||||
- The check should be case-insensitive.
|
||||
- The base can be any number 2-36.
|
||||
- A number is valid if every character is a valid digit in the given base.
|
||||
- Example of valid digits for bases:
|
||||
- Base 2: 0-1
|
||||
- Base 8: 0-7
|
||||
- Base 10: 0-9
|
||||
- Base 16: 0-9 and A-F
|
||||
- Base 36: 0-9 and A-Z
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_valid_number("10101", 2)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("10101", 2), True)`);
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("10201", 2)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("10201", 2), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("76543210", 8)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("76543210", 8), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("9876543210", 8)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("9876543210", 8), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("9876543210", 10)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("9876543210", 10), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("ABC", 10)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("ABC", 10), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("ABC", 16)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("ABC", 16), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("Z", 36)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("Z", 36), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("ABC", 20)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("ABC", 20), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("4B4BA9", 16)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("4B4BA9", 16), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("5G3F8F", 16)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("5G3F8F", 16), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("5G3F8F", 17)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("5G3F8F", 17), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("abc", 10)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("abc", 10), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("abc", 16)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("abc", 16), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("AbC", 16)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("AbC", 16), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_number("z", 36)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_number("z", 36), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_valid_number(n, base):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_valid_number(n, base):
|
||||
allChars = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
newN = n.lower()
|
||||
|
||||
availableChars = allChars[0:base]
|
||||
|
||||
for char in newN:
|
||||
if char not in availableChars:
|
||||
return False
|
||||
|
||||
return True
|
||||
```
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 681cb1a2dab50c87ddb2e514
|
||||
title: "Challenge 3: Fibonacci Sequence"
|
||||
challengeType: 29
|
||||
dashedName: challenge-3
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. When starting with `0` and `1`, the first 10 numbers in the sequence are `0`, `1`, `1`, `2`, `3`, `5`, `8`, `13`, `21`, `34`.
|
||||
|
||||
Given an array containing the first two numbers of a Fibonacci sequence, and an integer representing the length of the sequence, return an array containing the sequence of the given length.
|
||||
|
||||
- Your function should handle sequences of any length greater than or equal to zero.
|
||||
- If the length is zero, return an empty array.
|
||||
- Note that the starting numbers are part of the sequence.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
`fibonacci_sequence([0, 1], 20)` should return `[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(fibonacci_sequence([0, 1], 20), [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`fibonacci_sequence([21, 32], 1)` should return `[21]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(fibonacci_sequence([21, 32], 1), [21])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`fibonacci_sequence([0, 1], 0)` should return `[]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(fibonacci_sequence([0, 1], 0), [])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`fibonacci_sequence([10, 20], 2)` should return `[10, 20]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(fibonacci_sequence([10, 20], 2), [10, 20])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`fibonacci_sequence([123456789, 987654321], 5)` should return `[123456789, 987654321, 1111111110, 2098765431, 3209876541]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(fibonacci_sequence([123456789, 987654321], 5), [123456789, 987654321, 1111111110, 2098765431, 3209876541])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def fibonacci_sequence(start_sequence, length):
|
||||
|
||||
return length
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def fibonacci_sequence(start_sequence, length):
|
||||
if length == 0:
|
||||
return []
|
||||
if length == 1:
|
||||
return [start_sequence[0]]
|
||||
if length == 2:
|
||||
return start_sequence[:]
|
||||
|
||||
sequence = start_sequence[:]
|
||||
while len(sequence) < length:
|
||||
next_value = sequence[-1] + sequence[-2]
|
||||
sequence.append(next_value)
|
||||
|
||||
return sequence
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 681cb1afdab50c87ddb2e515
|
||||
title: "Challenge 4: S P A C E J A M"
|
||||
challengeType: 29
|
||||
dashedName: challenge-4
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, remove all spaces from the string, insert two spaces between every character, convert all alphabetical letters to uppercase, and return the result.
|
||||
|
||||
- Non-alphabetical characters should remain unchanged (except for spaces).
|
||||
|
||||
# --hints--
|
||||
|
||||
`space_jam("freeCodeCamp")` should return `"F R E E C O D E C A M P"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(space_jam("freeCodeCamp"), "F R E E C O D E C A M P")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`space_jam(" free Code Camp ")` should return `"F R E E C O D E C A M P"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(space_jam(" free Code Camp "), "F R E E C O D E C A M P")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`space_jam("Hello World?!")` should return `"H E L L O W O R L D ? !"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(space_jam("Hello World?!"), "H E L L O W O R L D ? !")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`space_jam("C@t$ & D0g$")` should return `"C @ T $ & D 0 G $"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(space_jam("C@t$ & D0g$"), "C @ T $ & D 0 G $")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`space_jam("allyourbase")` should return `"A L L Y O U R B A S E"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(space_jam("all your base"), "A L L Y O U R B A S E")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def space_jam(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def space_jam(s):
|
||||
s = s.replace(" ", "")
|
||||
s = " ".join(s)
|
||||
return s.upper()
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 681cb1afdab50c87ddb2e516
|
||||
title: "Challenge 5: Jbelmud Text"
|
||||
challengeType: 29
|
||||
dashedName: challenge-5
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return a jumbled version of that string where each word is transformed using the following constraints:
|
||||
|
||||
- The first and last letters of the words remain in place
|
||||
- All letters between the first and last letter are sorted alphabetically.
|
||||
- The input strings will contain no punctuation, and will be entirely lowercase.
|
||||
|
||||
# --hints--
|
||||
|
||||
`jbelmu("hello world")` should return `"hello wlord"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(jbelmu("hello world"), "hello wlord")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`jbelmu("i love jumbled text")` should return `"i love jbelmud text"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(jbelmu("i love jumbled text"), "i love jbelmud text")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`jbelmu("freecodecamp is my favorite place to learn to code")` should return `"faccdeeemorp is my faiortve pacle to laern to cdoe"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(jbelmu("freecodecamp is my favorite place to learn to code"), "faccdeeemorp is my faiortve pacle to laern to cdoe")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`jbelmu("the quick brown fox jumps over the lazy dog")` should return `"the qciuk borwn fox jmpus oevr the lazy dog"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(jbelmu("the quick brown fox jumps over the lazy dog"), "the qciuk borwn fox jmpus oevr the lazy dog")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def jbelmu(text):
|
||||
|
||||
return text
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def jbelmu(text):
|
||||
words = text.split()
|
||||
jumbled = []
|
||||
|
||||
for word in words:
|
||||
if len(word) <= 3:
|
||||
jumbled.append(word)
|
||||
else:
|
||||
first = word[0]
|
||||
last = word[-1]
|
||||
middle = ''.join(sorted(word[1:-1]))
|
||||
jumbled.append(first + middle + last)
|
||||
|
||||
return ' '.join(jumbled)
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 681cb1afdab50c87ddb2e517
|
||||
title: "Challenge 6: Anagram Checker"
|
||||
challengeType: 29
|
||||
dashedName: challenge-6
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two strings, determine if they are anagrams of each other (contain the same characters in any order).
|
||||
|
||||
- Ignore casing and white space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`are_anagrams("listen", "silent")` should return `true`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("listen", "silent"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`are_anagrams("School master", "The classroom")` should return `true`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("School master", "The classroom"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`are_anagrams("A gentleman", "Elegant man")` should return `true`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("A gentleman", "Elegant man"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`are_anagrams("Hello", "World")` should return `false`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("Hello", "World"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`are_anagrams("apple", "banana")` should return `false`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("apple", "banana"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`are_anagrams("cat", "dog")` should return `false`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(are_anagrams("cat", "dog"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def are_anagrams(str1, str2):
|
||||
|
||||
return str1
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def are_anagrams(str1, str2):
|
||||
def clean(s):
|
||||
return sorted(s.replace(" ", "").lower())
|
||||
|
||||
return clean(str1) == clean(str2)
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 681cb1b0dab50c87ddb2e518
|
||||
title: "Challenge 7: Targeted Sum"
|
||||
challengeType: 29
|
||||
dashedName: challenge-7
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of numbers and an integer target, find two unique numbers in the array that add up to the target value. Return an array with the indices of those two numbers, or `"Target not found"` if no two numbers sum up to the target.
|
||||
|
||||
- The returned array should have the indices in ascending order.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_target([2, 7, 11, 15], 9)` should return `[0, 1]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_target([2, 7, 11, 15], 9), [0, 1])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_target([3, 2, 4, 5], 6)` should return `[1, 2]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_target([3, 2, 4, 5], 6), [1, 2])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_target([1, 3, 5, 6, 7, 8], 15)` should return `[4, 5]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_target([1, 3, 5, 6, 7, 8], 15), [4, 5])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_target([1, 3, 5, 7], 14)` should return `'Target not found'`.
|
||||
|
||||
```js
|
||||
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_target([1, 3, 5, 7], 14), "Target not found")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_target(arr, target):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_target(arr, target):
|
||||
for i in range(len(arr)):
|
||||
for j in range(i + 1, len(arr)):
|
||||
if arr[i] + arr[j] == target:
|
||||
return [i, j]
|
||||
return 'Target not found'
|
||||
```
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 681cb1b0dab50c87ddb2e519
|
||||
title: "Challenge 8: Factorializer"
|
||||
challengeType: 29
|
||||
dashedName: challenge-8
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer from zero to 20, return the factorial of that number. The factorial of a number is the product of all the numbers between 1 and the given number.
|
||||
|
||||
- The factorial of zero is 1.
|
||||
|
||||
# --hints--
|
||||
|
||||
`factorial(0)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(factorial(0), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`factorial(5)` should return `120`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(factorial(5), 120)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`factorial(20)` should return `2432902008176640000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(factorial(20), 2432902008176640000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def factorial(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def factorial(n):
|
||||
result = 1
|
||||
for i in range(1, n + 1):
|
||||
result *= i
|
||||
return result
|
||||
```
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 681cb1b0dab50c87ddb2e51a
|
||||
title: "Challenge 9: Sum of Squares"
|
||||
challengeType: 29
|
||||
dashedName: challenge-9
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a positive integer up to 1,000, return the sum of all the integers squared from 1 up to the number.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sum_of_squares(5)` should return `55`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sum_of_squares(5), 55)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sum_of_squares(10)` should return `385`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sum_of_squares(10), 385)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sum_of_squares(25)` should return `5525`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sum_of_squares(25), 5525)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sum_of_squares(500)` should return `41791750`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sum_of_squares(500), 41791750)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sum_of_squares(1000)` should return `333833500`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sum_of_squares(1000), 333833500)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sum_of_squares(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sum_of_squares(n):
|
||||
sum = ((n)*(n+1)*(2*n+1))//6
|
||||
return sum
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 681cb1b0dab50c87ddb2e51b
|
||||
title: "Challenge 10: 3 Strikes"
|
||||
challengeType: 29
|
||||
dashedName: challenge-10
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer between 1 and 10,000, return a count of how many numbers from 1 up to that integer whose square contains at least one digit 3.
|
||||
|
||||
# --hints--
|
||||
|
||||
`squares_with_three(1)` should return `0`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(squares_with_three(1), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`squares_with_three(10)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(squares_with_three(10), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`squares_with_three(100)` should return `19`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(squares_with_three(100), 19)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`squares_with_three(1000)` should return `326`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(squares_with_three(1000), 326)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`squares_with_three(10000)` should return `4531`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(squares_with_three(10000), 4531)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def squares_with_three(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def squares_with_three(n):
|
||||
count = 0
|
||||
for i in range(1, n + 1):
|
||||
square = i * i
|
||||
if '3' in str(square):
|
||||
count += 1
|
||||
return count
|
||||
```
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 68216eb60f957572e7c340c4
|
||||
title: "Challenge 11: Mile Pace"
|
||||
challengeType: 29
|
||||
dashedName: challenge-11
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a number of miles ran, and a time in `"MM:SS"` (minutes:seconds) it took to run those miles, return a string for the average time it took to run each mile in the format `"MM:SS"`.
|
||||
|
||||
- Add leading zeros when needed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`mile_pace(3, "24:00")` should return `"08:00"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mile_pace(3, "24:00"), "08:00")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mile_pace(1, "06:45")` should return `"06:45"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mile_pace(1, "06:45"), "06:45")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mile_pace(2, "07:00")` should return `"03:30"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mile_pace(2, "07:00"), "03:30")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mile_pace(26.2, "120:35")` should return `"04:36"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mile_pace(26.2, "120:35"), "04:36")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def mile_pace(miles, duration):
|
||||
|
||||
return miles
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def mile_pace(miles, duration):
|
||||
minutes, seconds = map(int, duration.split(":"))
|
||||
total_seconds = minutes * 60 + seconds
|
||||
avg_seconds_per_mile = total_seconds / miles
|
||||
|
||||
avg_minutes = int(avg_seconds_per_mile // 60)
|
||||
avg_seconds = round(avg_seconds_per_mile % 60)
|
||||
|
||||
return f"{avg_minutes:02d}:{avg_seconds:02d}"
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 68216ef80f957572e7c340c5
|
||||
title: "Challenge 12: Message Decoder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-12
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a secret message string, and an integer representing the number of letters that were used to shift the message to encode it, return the decoded string.
|
||||
|
||||
- A positive number means the message was shifted forward in the alphabet.
|
||||
- A negative number means the message was shifted backward in the alphabet.
|
||||
- Case matters, decoded characters should retain the case of their encoded counterparts.
|
||||
- Non-alphabetical characters should not get decoded.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decode("Xlmw mw e wigvix qiwweki.", 4)` should return `"This is a secret message."`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("Xlmw mw e wigvix qiwweki.", 4), "This is a secret message.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("Byffi Qilfx!", 20)` should return `"Hello World!"`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("Byffi Qilfx!", 20), "Hello World!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("Zqd xnt njzx?", -1)` should return `"Are you okay?"`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("Zqd xnt njzx?", -1), "Are you okay?")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("oannLxmnLjvy", 9)` should return `"freeCodeCamp"`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("oannLxmnLjvy", 9), "freeCodeCamp")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def decode(message, shift):
|
||||
|
||||
return message
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def decode(message, shift):
|
||||
decoded_message = []
|
||||
for char in message:
|
||||
if char.isalpha():
|
||||
base = ord('a') if char.islower() else ord('A')
|
||||
new_char = chr((ord(char) - base - shift) % 26 + base)
|
||||
decoded_message.append(new_char)
|
||||
else:
|
||||
decoded_message.append(char)
|
||||
return ''.join(decoded_message)
|
||||
```
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
---
|
||||
id: 6821ebc9237de8297eaee78f
|
||||
title: "Challenge 13: Unnatural Prime"
|
||||
challengeType: 29
|
||||
dashedName: challenge-13
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer, determine if that number is a prime number or a negative prime number.
|
||||
|
||||
- A prime number is a positive integer greater than 1 that is only divisible by 1 and itself.
|
||||
- A negative prime number is the negative version of a positive prime number.
|
||||
- `1` and `0` are not considered prime numbers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_unnatural_prime(1)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(1), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(-1)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(-1), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(19)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(19), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(-23)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(-23), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(0)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(0), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(97)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(97), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(-61)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(-61), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(99)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(99), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_unnatural_prime(-44)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_unnatural_prime(-44), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_unnatural_prime(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_unnatural_prime(n):
|
||||
abs_n = abs(n)
|
||||
|
||||
if abs_n <= 1:
|
||||
return False
|
||||
|
||||
for i in range(2, int(abs_n ** 0.5) + 1):
|
||||
if abs_n % i == 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
```
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
---
|
||||
id: 6821ebce237de8297eaee790
|
||||
title: "Challenge 14: Character Battle"
|
||||
challengeType: 29
|
||||
dashedName: challenge-14
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two strings representing your army and an opposing army, each character from your army battles the character at the same position from the opposing army using the following rules:
|
||||
|
||||
- Characters `a-z` have a strength of `1-26`, respectively.
|
||||
- Characters `A-Z` have a strength of `27-52`, respectively.
|
||||
- Digits `0-9` have a strength of their face value.
|
||||
- All other characters have a value of zero.
|
||||
- Each character can only fight one battle.
|
||||
|
||||
For each battle, the stronger character wins. The army with more victories, wins the war. Return the following values:
|
||||
|
||||
- `"Opponent retreated"` if your army has more characters than the opposing army.
|
||||
- `"We retreated"` if the opposing army has more characters than yours.
|
||||
- `"We won"` if your army won more battles.
|
||||
- `"We lost"` if the opposing army won more battles.
|
||||
- `"It was a tie"` if both armies won the same number of battles.
|
||||
|
||||
# --hints--
|
||||
|
||||
`battle("Hello", "World")` should return `"We lost"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("Hello", "World"), "We lost")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("pizza", "salad")` should return `"We won"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("pizza", "salad"), "We won")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("C@T5", "D0G$")` should return `"We won"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("C@T5", "D0G$"), "We won")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("kn!ght", "orc")` should return `"Opponent retreated"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("kn!ght", "orc"), "Opponent retreated")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("PC", "Mac")` should return `"We retreated"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("PC", "Mac"), "We retreated")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("Wizards", "Dragons")` should return `"It was a tie"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("Wizards", "Dragons"), "It was a tie")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("Mr. Smith", "Dr. Jones")` should return `"It was a tie"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("Mr. Smith", "Dr. Jones"), "It was a tie")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def battle(my_army, opposing_army):
|
||||
|
||||
return my_army
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_strength(soldier):
|
||||
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
||||
if soldier.isdigit():
|
||||
return int(soldier)
|
||||
elif soldier in letters:
|
||||
return letters.index(soldier) + 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def battle(my_army, opposing_army):
|
||||
if len(my_army) > len(opposing_army):
|
||||
return 'Opponent retreated'
|
||||
if len(opposing_army) > len(my_army):
|
||||
return 'We retreated'
|
||||
|
||||
my_wins = 0
|
||||
their_wins = 0
|
||||
|
||||
for my_soldier, their_soldier in zip(my_army, opposing_army):
|
||||
my_strength = get_strength(my_soldier)
|
||||
their_strength = get_strength(their_soldier)
|
||||
|
||||
if my_strength > their_strength:
|
||||
my_wins += 1
|
||||
elif their_strength > my_strength:
|
||||
their_wins += 1
|
||||
|
||||
if my_wins > their_wins:
|
||||
return 'We won'
|
||||
elif their_wins > my_wins:
|
||||
return 'We lost'
|
||||
else:
|
||||
return 'It was a tie'
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 6821ebd4237de8297eaee791
|
||||
title: "Challenge 15: camelCase"
|
||||
challengeType: 29
|
||||
dashedName: challenge-15
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return its camel case version using the following rules:
|
||||
|
||||
- Words in the string argument are separated by one or more characters from the following set: space (` `), dash (`-`), or underscore (`_`). Treat any sequence of these as a word break.
|
||||
- The first word should be all lowercase.
|
||||
- Each subsequent word should start with an uppercase letter, with the rest of it lowercase.
|
||||
- All spaces and separators should be removed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`to_camel_case("hello world")` should return `"helloWorld"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_camel_case("hello world"), "helloWorld")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_camel_case("HELLO WORLD")` should return `"helloWorld"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_camel_case("HELLO WORLD"), "helloWorld")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_camel_case("secret agent-X")` should return `"secretAgentX"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_camel_case("secret agent-X"), "secretAgentX")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_camel_case("FREE cODE cAMP")` should return `"freeCodeCamp"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_camel_case("FREE cODE cAMP"), "freeCodeCamp")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_camel_case("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk")` should return `"yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_camel_case("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk"), "yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def to_camel_case(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def to_camel_case(s):
|
||||
words = re.split(r'[_\- ]+', s)
|
||||
|
||||
camel = [
|
||||
words[0].lower() if words else ''
|
||||
] + [
|
||||
word.capitalize() for word in words[1:]
|
||||
]
|
||||
|
||||
return ''.join(camel)
|
||||
```
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 6821ebda237de8297eaee792
|
||||
title: "Challenge 16: Reverse Parenthesis"
|
||||
challengeType: 29
|
||||
dashedName: challenge-16
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string that contains properly nested parentheses, return the decoded version of the string using the following rules:
|
||||
|
||||
- All characters inside each pair of parentheses should be reversed.
|
||||
- Parentheses should be removed from the final result.
|
||||
- If parentheses are nested, the innermost pair should be reversed first, and then its result should be included in the reversal of the outer pair.
|
||||
- Assume all parentheses are evenly balanced and correctly nested.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decode("(f(b(dc)e)a)")` should return `"abcdef"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("(f(b(dc)e)a)"), "abcdef")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("((is?)(a(t d)h)e(n y( uo)r)aC)")` should return `"Can you read this?"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("((is?)(a(t d)h)e(n y( uo)r)aC)"), "Can you read this?")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("f(Ce(re))o((e(aC)m)d)p")` should return `"freeCodeCamp"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("f(Ce(re))o((e(aC)m)d)p"), "freeCodeCamp")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def decode(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def decode(s):
|
||||
while ')' in s:
|
||||
close_index = s.index(')')
|
||||
open_index = s.rindex('(', 0, close_index)
|
||||
inner = s[open_index + 1:close_index][::-1]
|
||||
s = s[:open_index] + inner + s[close_index + 1:]
|
||||
return s
|
||||
```
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 6821ebdf237de8297eaee793
|
||||
title: "Challenge 17: Unorder of Operations"
|
||||
challengeType: 29
|
||||
dashedName: challenge-17
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers and an array of string operators, apply the operations to the numbers sequentially from left-to-right. Repeat the operations as needed until all numbers are used. Return the final result.
|
||||
|
||||
For example, given `[1, 2, 3, 4, 5]` and `['+', '*']`, return the result of evaluating `1 + 2 * 3 + 4 * 5` from left-to-right ignoring standard order of operations.
|
||||
|
||||
- Valid operators are `+`, `-`, `*`, `/`, and `%`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`evaluate([5, 6, 7, 8, 9], ['+', '-'])` should return `3`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(evaluate([5, 6, 7, 8, 9], ['+', '-']), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`evaluate([17, 61, 40, 24, 38, 14], ['+', '%'])` should return `38`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(evaluate([17, 61, 40, 24, 38, 14], ['+', '%']), 38)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`evaluate([20, 2, 4, 24, 12, 3], ['*', '/'])` should return `60`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(evaluate([20, 2, 4, 24, 12, 3], ['*', '/']), 60)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`evaluate([11, 4, 10, 17, 2], ['*', '*', '%'])` should return `30`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(evaluate([11, 4, 10, 17, 2], ['*', '*', '%']), 30)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`evaluate([33, 11, 29, 13], ['/', '-'])` should return `-2`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(evaluate([33, 11, 29, 13], ['/', '-']), -2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def evaluate(numbers, operators):
|
||||
|
||||
return numbers
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def do_math(a, b, operator):
|
||||
if operator == '+':
|
||||
return a + b
|
||||
elif operator == '-':
|
||||
return a - b
|
||||
elif operator == '*':
|
||||
return a * b
|
||||
elif operator == '/':
|
||||
return a / b
|
||||
else:
|
||||
return a % b
|
||||
|
||||
def evaluate(numbers, operators):
|
||||
total = numbers[0]
|
||||
|
||||
for i in range(1, len(numbers)):
|
||||
operator = operators[(i - 1) % len(operators)]
|
||||
total = do_math(total, numbers[i], operator)
|
||||
|
||||
return total
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 6821ebe4237de8297eaee794
|
||||
title: "Challenge 18: Second Best"
|
||||
challengeType: 29
|
||||
dashedName: challenge-18
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers representing the price of different laptops, and an integer representing your budget, return:
|
||||
|
||||
1. The second most expensive laptop if it is within your budget, or
|
||||
2. The most expensive laptop that is within your budget, or
|
||||
3. `0` if no laptops are within your budget.
|
||||
|
||||
- Duplicate prices should be ignored.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_laptop_cost([1500, 2000, 1800, 1400], 1900)` should return `1800`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_laptop_cost([1500, 2000, 1800, 1400], 1900), 1800)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_laptop_cost([1500, 2000, 2000, 1800, 1400], 1900)` should return `1800`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_laptop_cost([1500, 2000, 2000, 1800, 1400], 1900), 1800)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_laptop_cost([2099, 1599, 1899, 1499], 2200)` should return `1899`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_laptop_cost([2099, 1599, 1899, 1499], 2200), 1899)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_laptop_cost([2099, 1599, 1899, 1499], 1000)` should return `0`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_laptop_cost([2099, 1599, 1899, 1499], 1000), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_laptop_cost([1200, 1500, 1600, 1800, 1400, 2000], 1450)` should return `1400`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_laptop_cost([1200, 1500, 1600, 1800, 1400, 2000], 1450), 1400)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_laptop_cost(laptops, budget):
|
||||
|
||||
return laptops
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_laptop_cost(laptops, budget):
|
||||
laptops = sorted(set(laptops), reverse=True)
|
||||
|
||||
if len(laptops) >= 2 and budget >= laptops[1]:
|
||||
return laptops[1]
|
||||
if not laptops or budget < laptops[-1]:
|
||||
return 0
|
||||
|
||||
for price in laptops[2:]:
|
||||
if budget >= price:
|
||||
return price
|
||||
|
||||
return 0
|
||||
```
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 6821ebea237de8297eaee795
|
||||
title: "Challenge 19: Candlelight"
|
||||
challengeType: 29
|
||||
dashedName: challenge-19
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer representing the number of candles you start with, and an integer representing how many burned candles it takes to create a new one, return the number of candles you will have used after creating and burning as many as you can.
|
||||
|
||||
For example, if given 7 candles and it takes 2 burned candles to make a new one:
|
||||
|
||||
1. Burn 7 candles to get 7 leftovers,
|
||||
2. Recycle 6 leftovers into 3 new candles (1 leftover remains),
|
||||
3. Burn 3 candles to get 3 more leftovers (4 total),
|
||||
4. Recycle 4 leftovers into 2 new candles,
|
||||
5. Burn 2 candles to get 2 leftovers,
|
||||
6. Recycle 2 leftovers into 1 new candle,
|
||||
7. Burn 1 candle.
|
||||
|
||||
You will have burned 13 total candles in the example.
|
||||
|
||||
# --hints--
|
||||
|
||||
`burn_candles(7, 2)` should return `13`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(burn_candles(7, 2), 13)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`burn_candles(10, 5)` should return `12`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(burn_candles(10, 5), 12)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`burn_candles(20, 3)` should return `29`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(burn_candles(20, 3), 29)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`burn_candles(17, 4)` should return `22`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(burn_candles(17, 4), 22)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`burn_candles(2345, 3)` should return `3517`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(burn_candles(2345, 3), 3517)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def burn_candles(candles, leftovers_needed):
|
||||
|
||||
return candles
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def burn_candles(candles, leftovers_needed):
|
||||
total_burned = 0
|
||||
unused_leftovers = 0
|
||||
|
||||
while candles > 0:
|
||||
total_burned += candles
|
||||
leftovers = candles + unused_leftovers
|
||||
candles = leftovers // leftovers_needed
|
||||
unused_leftovers = leftovers % leftovers_needed
|
||||
|
||||
return total_burned
|
||||
```
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 6821ebee237de8297eaee796
|
||||
title: "Challenge 20: Array Duplicates"
|
||||
challengeType: 29
|
||||
dashedName: challenge-20
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers, return an array of integers that appear more than once in the initial array, sorted in ascending order. If no values appear more than once, return an empty array.
|
||||
|
||||
- Only include one instance of each value in the returned array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_duplicates([1, 2, 3, 4, 5])` should return `[]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_duplicates([1, 2, 3, 4, 5]), [])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_duplicates([1, 2, 3, 4, 1, 2])` should return `[1, 2]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_duplicates([1, 2, 3, 4, 1, 2]), [1, 2])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_duplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4])` should return `[-6, 0, 2, 4, 5, 23]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_duplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4]), [-6, 0, 2, 4, 5, 23])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_duplicates(arr):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_duplicates(arr):
|
||||
seen = set()
|
||||
duplicates = set()
|
||||
|
||||
for num in arr:
|
||||
if num in seen:
|
||||
duplicates.add(num)
|
||||
else:
|
||||
seen.add(num)
|
||||
|
||||
return sorted(duplicates)
|
||||
```
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
---
|
||||
id: 6821ebf3237de8297eaee797
|
||||
title: "Challenge 21: Hex Generator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-21
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a named CSS color string, generate a random hexadecimal (hex) color code that is dominant in the given color.
|
||||
|
||||
- The function should handle `"red"`, `"green"`, or `"blue"` as an input argument.
|
||||
- If the input is not one of those, the function should return `"Invalid color"`.
|
||||
- The function should return a random six-character hex color code where the input color value is greater than any of the others.
|
||||
- Example of valid outputs for a given input:
|
||||
|
||||
| Input | Output |
|
||||
|---------|----------|
|
||||
| `"red"` | `"FF0000"` |
|
||||
| `"green"` | `"00FF00"` |
|
||||
| `"blue"` | `"0000FF"` |
|
||||
|
||||
# --hints--
|
||||
|
||||
`generate_hex("yellow")` should return `"Invalid color"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_hex("yellow"), "Invalid color")`);
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_hex("red")` should return a six-character string.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(len(generate_hex("red")), 6)`);
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_hex("red")` should return a valid six-character hex color code.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
|
||||
hex = generate_hex("red").upper()
|
||||
is_valid_hex = len(hex) == 6 and all(c in "0123456789ABCDEF" for c in hex)
|
||||
TestCase().assertTrue(is_valid_hex)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_hex("red")` should return a valid hex color with a higher red value than other colors.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
|
||||
hex = generate_hex("red").upper()
|
||||
is_valid_hex = len(hex) == 6 and all(c in "0123456789ABCDEF" for c in hex)
|
||||
TestCase().assertTrue(is_valid_hex)
|
||||
|
||||
r = int(hex[:2], 16)
|
||||
g = int(hex[2:4], 16)
|
||||
b = int(hex[4:], 16)
|
||||
|
||||
TestCase().assertGreater(r, g)
|
||||
TestCase().assertGreater(r, b)`)
|
||||
}})
|
||||
```
|
||||
|
||||
Calling `generate_hex("red")` twice should return two different hex color values where red is dominant.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
|
||||
hex1 = generate_hex("red").upper()
|
||||
is_valid_hex1 = len(hex1) == 6 and all(c in "0123456789ABCDEF" for c in hex1)
|
||||
TestCase().assertTrue(is_valid_hex1)
|
||||
|
||||
r1 = int(hex1[:2], 16)
|
||||
g1 = int(hex1[2:4], 16)
|
||||
b1 = int(hex1[4:], 16)
|
||||
|
||||
TestCase().assertGreater(r1, g1)
|
||||
TestCase().assertGreater(r1, b1)
|
||||
|
||||
hex2 = generate_hex("red").upper()
|
||||
is_valid_hex2 = len(hex2) == 6 and all(c in "0123456789ABCDEF" for c in hex2)
|
||||
TestCase().assertTrue(is_valid_hex2)
|
||||
|
||||
r2 = int(hex2[:2], 16)
|
||||
g2 = int(hex2[2:4], 16)
|
||||
b2 = int(hex2[4:], 16)
|
||||
|
||||
TestCase().assertGreater(r2, g2)
|
||||
TestCase().assertGreater(r2, b2)
|
||||
TestCase().assertNotEqual(hex1, hex2)`);
|
||||
}})
|
||||
```
|
||||
|
||||
Calling `generate_hex("green")` twice should return two different hex color values where green is dominant.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
|
||||
hex1 = generate_hex("green").upper()
|
||||
is_valid_hex1 = len(hex1) == 6 and all(c in "0123456789ABCDEF" for c in hex1)
|
||||
TestCase().assertTrue(is_valid_hex1)
|
||||
|
||||
r1 = int(hex1[:2], 16)
|
||||
g1 = int(hex1[2:4], 16)
|
||||
b1 = int(hex1[4:], 16)
|
||||
|
||||
TestCase().assertGreater(g1, r1)
|
||||
TestCase().assertGreater(g1, b1)
|
||||
|
||||
hex2 = generate_hex("green").upper()
|
||||
is_valid_hex2 = len(hex2) == 6 and all(c in "0123456789ABCDEF" for c in hex2)
|
||||
TestCase().assertTrue(is_valid_hex2)
|
||||
|
||||
r2 = int(hex2[:2], 16)
|
||||
g2 = int(hex2[2:4], 16)
|
||||
b2 = int(hex2[4:], 16)
|
||||
|
||||
TestCase().assertGreater(g2, r2)
|
||||
TestCase().assertGreater(g2, b2)
|
||||
TestCase().assertNotEqual(hex1, hex2)`);
|
||||
}})
|
||||
```
|
||||
|
||||
Calling `generate_hex("blue")` twice should return two different hex color values where blue is dominant.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
|
||||
hex1 = generate_hex("blue").upper()
|
||||
is_valid_hex1 = len(hex1) == 6 and all(c in "0123456789ABCDEF" for c in hex1)
|
||||
TestCase().assertTrue(is_valid_hex1)
|
||||
|
||||
r1 = int(hex1[:2], 16)
|
||||
g1 = int(hex1[2:4], 16)
|
||||
b1 = int(hex1[4:], 16)
|
||||
|
||||
TestCase().assertGreater(b1, r1)
|
||||
TestCase().assertGreater(b1, g1)
|
||||
|
||||
hex2 = generate_hex("blue").upper()
|
||||
is_valid_hex2 = len(hex2) == 6 and all(c in "0123456789ABCDEF" for c in hex2)
|
||||
TestCase().assertTrue(is_valid_hex2)
|
||||
|
||||
r2 = int(hex2[:2], 16)
|
||||
g2 = int(hex2[2:4], 16)
|
||||
b2 = int(hex2[4:], 16)
|
||||
|
||||
TestCase().assertGreater(b2, r2)
|
||||
TestCase().assertGreater(b2, g2)
|
||||
TestCase().assertNotEqual(hex1, hex2)`);
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def generate_hex(color):
|
||||
|
||||
return color
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import random
|
||||
def generate_hex(color):
|
||||
def to_hex(n):
|
||||
return hex(n)[2:].upper().zfill(2)
|
||||
|
||||
dominant = random.randint(170, 255)
|
||||
weak1 = random.randint(0, 169)
|
||||
weak2 = random.randint(0, 169)
|
||||
|
||||
if color.lower() == "red":
|
||||
r = dominant
|
||||
g = weak1
|
||||
b = weak2
|
||||
elif color.lower() == "green":
|
||||
r = weak1
|
||||
g = dominant
|
||||
b = weak2
|
||||
elif color.lower() == "blue":
|
||||
r = weak1
|
||||
g = weak2
|
||||
b = dominant
|
||||
else:
|
||||
return "Invalid color"
|
||||
|
||||
return f'{to_hex(r)}{to_hex(g)}{to_hex(b)}'
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 6821ebf8237de8297eaee798
|
||||
title: "Challenge 22: Tribonacci Sequence"
|
||||
challengeType: 29
|
||||
dashedName: challenge-22
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The Tribonacci sequence is a series of numbers where each number is the sum of the three preceding ones. When starting with `0`, `0` and `1`, the first 10 numbers in the sequence are `0`, `0`, `1`, `1`, `2`, `4`, `7`, `13`, `24`, `44`.
|
||||
|
||||
Given an array containing the first three numbers of a Tribonacci sequence, and an integer representing the length of the sequence, return an array containing the sequence of the given length.
|
||||
|
||||
- Your function should handle sequences of any length greater than or equal to zero.
|
||||
- If the length is zero, return an empty array.
|
||||
- Note that the starting numbers are part of the sequence.
|
||||
|
||||
# --hints--
|
||||
|
||||
`tribonacci_sequence([0, 0, 1], 20)` should return `[0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([0, 0, 1], 20), [0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`tribonacci_sequence([21, 32, 43], 1)` should return `[21]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([21, 32, 43], 1), [21])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`tribonacci_sequence([0, 0, 1], 0)` should return `[]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([0, 0, 1], 0), [])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`tribonacci_sequence([10, 20, 30], 2)` should return `[10, 20]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([10, 20, 30], 2), [10, 20])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`tribonacci_sequence([10, 20, 30], 3)` should return `[10, 20, 30]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([10, 20, 30], 3), [10, 20, 30])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`tribonacci_sequence([123, 456, 789], 8)` should return `[123, 456, 789, 1368, 2613, 4770, 8751, 16134]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(tribonacci_sequence([123, 456, 789], 8), [123, 456, 789, 1368, 2613, 4770, 8751, 16134])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def tribonacci_sequence(start_sequence, length):
|
||||
|
||||
return length
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def tribonacci_sequence(start_sequence, length):
|
||||
if length == 0:
|
||||
return []
|
||||
if length == 1:
|
||||
return [start_sequence[0]]
|
||||
if length == 2:
|
||||
return [start_sequence[0], start_sequence[1]]
|
||||
if length == 3:
|
||||
return start_sequence[:]
|
||||
|
||||
sequence = start_sequence[:]
|
||||
while len(sequence) < length:
|
||||
next_value = sequence[-1] + sequence[-2] + sequence[-3]
|
||||
sequence.append(next_value)
|
||||
|
||||
return sequence
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 6821ebfd237de8297eaee799
|
||||
title: "Challenge 23: RGB to Hex"
|
||||
challengeType: 29
|
||||
dashedName: challenge-23
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a CSS `rgb(r, g, b)` color string, return its hexadecimal equivalent.
|
||||
|
||||
Here are some example outputs for a given input:
|
||||
|
||||
| Input | Output |
|
||||
|---------|----------|
|
||||
| `"rgb(255, 255, 255)"`| `"#ffffff"` |
|
||||
| `"rgb(1, 2, 3)"` | `"#010203"` |
|
||||
|
||||
- Make any letters lowercase.
|
||||
- Return a `#` followed by six characters. Don't use any shorthand values.
|
||||
|
||||
# --hints--
|
||||
|
||||
`rgb_to_hex("rgb(255, 255, 255)")` should return `"#ffffff"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rgb_to_hex("rgb(255, 255, 255)"), "#ffffff")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rgb_to_hex("rgb(1, 11, 111)")` should return `"#010b6f"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rgb_to_hex("rgb(1, 11, 111)"), "#010b6f")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rgb_to_hex("rgb(173, 216, 230)")` should return `"#add8e6"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rgb_to_hex("rgb(173, 216, 230)"), "#add8e6")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rgb_to_hex("rgb(79, 123, 201)")` should return `"#4f7bc9"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rgb_to_hex("rgb(79, 123, 201)"), "#4f7bc9")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def rgb_to_hex(rgb):
|
||||
|
||||
return rgb
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def rgb_to_hex(rgb):
|
||||
import re
|
||||
match = re.findall(r'\d+', rgb)
|
||||
r, g, b = [max(0, min(255, int(x))) for x in match[:3]]
|
||||
return f'#{r:02x}{g:02x}{b:02x}'
|
||||
```
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 6821ec02237de8297eaee79a
|
||||
title: "Challenge 24: Pangram"
|
||||
challengeType: 29
|
||||
dashedName: challenge-24
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a word or sentence and a string of lowercase letters, determine if the word or sentence uses all the letters from the given set at least once and no other letters.
|
||||
|
||||
- Ignore non-alphabetical characters in the word or sentence.
|
||||
- Ignore letter casing in the word or sentence.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_pangram("hello", "helo")` should return `True`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("hello", "helo"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("hello", "hel")` should return `False`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("hello", "hel"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("hello", "helow")` should return `False`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("hello", "helow"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("hello world", "helowrd")` should return `True`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("hello world", "helowrd"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("Hello World!", "helowrd")` should return `True`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("Hello World!", "helowrd"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("Hello World!", "heliowrd")` should return `False`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("Hello World!", "heliowrd"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("freeCodeCamp", "frcdmp")` should return `False`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("freeCodeCamp", "frcdmp"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_pangram("The quick brown fox jumps over the lazy dog.", "abcdefghijklmnopqrstuvwxyz")` should return `True`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_pangram("The quick brown fox jumps over the lazy dog.", "abcdefghijklmnopqrstuvwxyz"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_pangram(sentence, letters):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def is_pangram(sentence, letters):
|
||||
used_letters = []
|
||||
for char in sentence.lower():
|
||||
if re.match(r'[a-z]', char) and char not in used_letters:
|
||||
used_letters.append(char)
|
||||
|
||||
sorted_letters = ''.join(sorted(letters.lower()))
|
||||
sorted_used_letters = ''.join(sorted(used_letters))
|
||||
|
||||
return sorted_letters == sorted_used_letters
|
||||
```
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 68adce01c0e1144d0a902956
|
||||
title: "Challenge 25: Vowel Repeater"
|
||||
challengeType: 29
|
||||
dashedName: challenge-25
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return a new version of the string where each vowel is duplicated one more time than the previous vowel you encountered. For instance, the first vowel in the sentence should remain unchanged. The second vowel should appear twice in a row. The third vowel should appear three times in a row, and so on.
|
||||
|
||||
- The letters `a`, `e`, `i`, `o`, and `u`, in either uppercase or lowercase, are considered vowels.
|
||||
- The original vowel should keeps its case.
|
||||
- Repeated vowels should be lowercase.
|
||||
- All non-vowel characters should keep their original case.
|
||||
|
||||
# --hints--
|
||||
|
||||
`repeat_vowels("hello world")` should return `"helloo wooorld"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(repeat_vowels("hello world"), "helloo wooorld")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`repeat_vowels("freeCodeCamp")` should return `"freeeCooodeeeeCaaaaamp"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(repeat_vowels("freeCodeCamp"), "freeeCooodeeeeCaaaaamp")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`repeat_vowels("AEIOU")` should return `"AEeIiiOoooUuuuu"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(repeat_vowels("AEIOU"), "AEeIiiOoooUuuuu")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`repeat_vowels("I like eating ice cream in Iceland")` should return `"I liikeee eeeeaaaaatiiiiiing iiiiiiiceeeeeeee creeeeeeeeeaaaaaaaaaam iiiiiiiiiiin Iiiiiiiiiiiiceeeeeeeeeeeeelaaaaaaaaaaaaaand"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(repeat_vowels("I like eating ice cream in Iceland"), "I liikeee eeeeaaaaatiiiiiing iiiiiiiceeeeeeee creeeeeeeeeaaaaaaaaaam iiiiiiiiiiin Iiiiiiiiiiiiceeeeeeeeeeeeelaaaaaaaaaaaaaand")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def repeat_vowels(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def repeat_vowels(s):
|
||||
vowels = "aeiouAEIOU"
|
||||
count = 0
|
||||
result = []
|
||||
|
||||
for char in s:
|
||||
result.append(char)
|
||||
if char in vowels:
|
||||
result.append(char.lower() * count)
|
||||
count += 1
|
||||
|
||||
return "".join(result)
|
||||
```
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
---
|
||||
id: 68adce01c0e1144d0a902958
|
||||
title: "Challenge 26: IPv4 Validator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-26
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, determine if it is a valid IPv4 Address. A valid IPv4 address consists of four integer numbers separated by dots (`.`). Each number must satisfy the following conditions:
|
||||
|
||||
- It is between 0 and 255 inclusive.
|
||||
- It does not have leading zeros (e.g. 0 is allowed, 01 is not).
|
||||
- Only numeric characters are allowed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_valid_ipv4("192.168.1.1")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("192.168.1.1"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("0.0.0.0")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("0.0.0.0"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("255.01.50.111")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("255.01.50.111"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("255.00.50.111")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("255.00.50.111"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("256.101.50.115")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("256.101.50.115"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("192.168.101.")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("192.168.101."), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_valid_ipv4("192168145213")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_valid_ipv4("192168145213"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_valid_ipv4(ipv4):
|
||||
|
||||
return ipv4
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_valid_ipv4(ipv4):
|
||||
parts = ipv4.split(".")
|
||||
|
||||
if len(parts) != 4:
|
||||
return False
|
||||
|
||||
for part in parts:
|
||||
if not part.isdigit():
|
||||
return False
|
||||
|
||||
num = int(part)
|
||||
if num < 0 or num > 255:
|
||||
return False
|
||||
|
||||
if len(part) > 1 and part.startswith("0"):
|
||||
return False
|
||||
|
||||
return True
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 68adce01c0e1144d0a90295a
|
||||
title: "Challenge 27: Matrix Rotate"
|
||||
challengeType: 29
|
||||
dashedName: challenge-27
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a matrix (an array of arrays), rotate the matrix 90 degrees clockwise and return it. For instance, given `[[1, 2], [3, 4]]`, which looks like this:
|
||||
|
||||
| 1 | 2 |
|
||||
|---|---|
|
||||
| 3 | 4 |
|
||||
|
||||
You should return `[[3, 1], [4, 2]]`, which looks like this:
|
||||
|
||||
| 3 | 1 |
|
||||
|---|---|
|
||||
| 4 | 2 |
|
||||
|
||||
# --hints--
|
||||
|
||||
`rotate([[1]])` should return `[[1]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rotate([[1]]), [[1]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rotate([[1, 2], [3, 4]])` should return `[[3, 1], [4, 2]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rotate([[1, 2], [3, 4]]), [[3, 1], [4, 2]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]])` should return `[[7, 4, 1], [8, 5, 2], [9, 6, 3]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [[7, 4, 1], [8, 5, 2], [9, 6, 3]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`rotate([[0, 1, 0], [1, 0, 1], [0, 0, 0]])` should return `[[0, 1, 0], [0, 0, 1], [0, 1, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(rotate([[0, 1, 0], [1, 0, 1], [0, 0, 0]]), [[0, 1, 0], [0, 0, 1], [0, 1, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def rotate(matrix):
|
||||
|
||||
return matrix
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def rotate(matrix):
|
||||
n = len(matrix)
|
||||
result = [[0] * n for _ in range(n)]
|
||||
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
result[j][n - 1 - i] = matrix[i][j]
|
||||
|
||||
return result
|
||||
```
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
---
|
||||
id: 68adce01c0e1144d0a90295c
|
||||
title: "Challenge 28: Roman Numeral Parser"
|
||||
challengeType: 29
|
||||
dashedName: challenge-28
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a Roman numeral, return its integer value.
|
||||
|
||||
Roman numerals consist of the following symbols and values:
|
||||
|
||||
| Symbol | Value |
|
||||
|--------|-------|
|
||||
| I | 1 |
|
||||
| V | 5 |
|
||||
| X | 10 |
|
||||
| L | 50 |
|
||||
| C | 100 |
|
||||
| D | 500 |
|
||||
| M | 1000 |
|
||||
|
||||
- Numerals are read left to right. If a smaller numeral appears before a larger one, the value is subtracted. Otherwise, values are added.
|
||||
|
||||
# --hints--
|
||||
|
||||
`parse_roman_numeral("III")` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("III"), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("IV")` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("IV"), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("XXVI")` should return `26`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("XXVI"), 26)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("XCIX")` should return `99`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("XCIX"), 99)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("CDLX")` should return `460`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("CDLX"), 460)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("DIV")` should return `504`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("DIV"), 504)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_roman_numeral("MMXXV")` should return `2025`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_roman_numeral("MMXXV"), 2025)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def parse_roman_numeral(numeral):
|
||||
|
||||
return numeral
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def parse_roman_numeral(numeral):
|
||||
roman_map = {
|
||||
"I": 1,
|
||||
"V": 5,
|
||||
"X": 10,
|
||||
"L": 50,
|
||||
"C": 100,
|
||||
"D": 500,
|
||||
"M": 1000
|
||||
}
|
||||
|
||||
total = 0
|
||||
for i in range(len(numeral)):
|
||||
current = roman_map[numeral[i]]
|
||||
next_val = roman_map[numeral[i + 1]] if i + 1 < len(numeral) else 0
|
||||
|
||||
if current < next_val:
|
||||
total -= current
|
||||
else:
|
||||
total += current
|
||||
|
||||
return total
|
||||
```
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 68adce01c0e1144d0a90295e
|
||||
title: "Challenge 29: Acronym Builder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-29
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string containing one or more words, return an acronym of the words using the following constraints:
|
||||
|
||||
- The acronym should consist of the first letter of each word capitalized, unless otherwise noted.
|
||||
- The acronym should ignore the first letter of these words unless they are the first word of the given string: `a`, `for`, `an`, `and`, `by`, and `of`.
|
||||
- The acronym letters should be returned in the order they are given.
|
||||
- The acronym should not contain any spaces.
|
||||
|
||||
# --hints--
|
||||
|
||||
`build_acronym("Search Engine Optimization")` should return `"SEO"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("Search Engine Optimization"), "SEO")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("Frequently Asked Questions")` should return `"FAQ"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("Frequently Asked Questions"), "FAQ")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("National Aeronautics and Space Administration")` should return `"NASA"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("National Aeronautics and Space Administration"), "NASA")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("Federal Bureau of Investigation")` should return `"FBI"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("Federal Bureau of Investigation"), "FBI")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("For your information")` should return `"FYI"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("For your information"), "FYI")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("By the way")` should return `"BTW"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("By the way"), "BTW")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_acronym("An unstoppable herd of waddling penguins overtakes the icy mountains and sings happily")` should return `"AUHWPOTIMSH"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_acronym("An unstoppable herd of waddling penguins overtakes the icy mountains and sings happily"), "AUHWPOTIMSH")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def build_acronym(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def build_acronym(s):
|
||||
small_words = {"a", "for", "an", "and", "by", "of"}
|
||||
words = s.split()
|
||||
acronym = ""
|
||||
|
||||
for i, word in enumerate(words):
|
||||
if i == 0 or word.lower() not in small_words:
|
||||
acronym += word[0].upper()
|
||||
|
||||
return acronym
|
||||
```
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
---
|
||||
id: 68af0687ef34c76c28ffa547
|
||||
title: "Challenge 30: Unique Characters"
|
||||
challengeType: 29
|
||||
dashedName: challenge-30
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, determine if all the characters in the string are unique.
|
||||
|
||||
- Uppercase and lowercase letters should be considered different characters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`all_unique("abc")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("abc"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("aA")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("aA"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("QwErTy123!@")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("QwErTy123!@"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("~!@#$%^&*()_+")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("~!@#$%^&*()_+"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("hello")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("hello"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("freeCodeCamp")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("freeCodeCamp"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`all_unique("!@#*$%^&*()aA")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(all_unique("!@#*$%^&*()aA"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def all_unique(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def all_unique(s):
|
||||
seen = set()
|
||||
for ch in s:
|
||||
if ch in seen:
|
||||
return False
|
||||
seen.add(ch)
|
||||
return True
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 68af0687ef34c76c28ffa549
|
||||
title: "Challenge 31: Array Diff"
|
||||
challengeType: 29
|
||||
dashedName: challenge-31
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two arrays with strings values, return a new array containing all the values that appear in only one of the arrays.
|
||||
|
||||
- The returned array should be sorted in alphabetical order.
|
||||
|
||||
# --hints--
|
||||
|
||||
`array_diff(["apple", "banana"], ["apple", "banana", "cherry"])` should return `["cherry"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(array_diff(["apple", "banana"], ["apple", "banana", "cherry"]), ["cherry"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`array_diff(["apple", "banana", "cherry"], ["apple", "banana"])` should return `["cherry"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(array_diff(["apple", "banana", "cherry"], ["apple", "banana"]), ["cherry"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`array_diff(["one", "two", "three", "four", "six"], ["one", "three", "eight"])` should return `["eight", "four", "six", "two"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(array_diff(["one", "two", "three", "four", "six"], ["one", "three", "eight"]), ["eight", "four", "six", "two"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`array_diff(["two", "four", "five", "eight"], ["one", "two", "three", "four", "seven", "eight"])` should return `["five", "one", "seven", "three"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(array_diff(["two", "four", "five", "eight"], ["one", "two", "three", "four", "seven", "eight"]), ["five", "one", "seven", "three"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`array_diff(["I", "like", "freeCodeCamp"], ["I", "like", "rocks"])` should return `["freeCodeCamp", "rocks"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(array_diff(["I", "like", "freeCodeCamp"], ["I", "like", "rocks"]), ["freeCodeCamp", "rocks"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def array_diff(arr1, arr2):
|
||||
|
||||
return arr1
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def array_diff(arr1, arr2):
|
||||
unique1 = []
|
||||
for v in arr1:
|
||||
if v not in unique1:
|
||||
unique1.append(v)
|
||||
unique2 = []
|
||||
for v in arr2:
|
||||
if v not in unique2:
|
||||
unique2.append(v)
|
||||
|
||||
only1 = [v for v in unique1 if v not in unique2]
|
||||
only2 = [v for v in unique2 if v not in unique1]
|
||||
|
||||
return sorted(only1 + only2)
|
||||
```
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 68af0687ef34c76c28ffa54b
|
||||
title: "Challenge 32: Reverse Sentence"
|
||||
challengeType: 29
|
||||
dashedName: challenge-32
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of words, return a new string with the words in reverse order. For example, the first word should be at the end of the returned string, and the last word should be at the beginning of the returned string.
|
||||
|
||||
- In the given string, words can be separated by one or more spaces.
|
||||
- The returned string should only have one space between words.
|
||||
|
||||
# --hints--
|
||||
|
||||
`reverse_sentence("world hello")` should return `"hello world"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(reverse_sentence("world hello"), "hello world")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`reverse_sentence("push commit git")` should return `"git commit push"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(reverse_sentence("push commit git"), "git commit push")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`reverse_sentence("npm install apt sudo")` should return `"sudo apt install npm"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(reverse_sentence("npm install apt sudo"), "sudo apt install npm")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`reverse_sentence("import default function export")` should return `"export function default import"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(reverse_sentence("import default function export"), "export function default import")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def reverse_sentence(sentence):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def reverse_sentence(sentence):
|
||||
|
||||
return " ".join(sentence.split()[::-1])
|
||||
```
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 68af0687ef34c76c28ffa54d
|
||||
title: "Challenge 33: Screen Time"
|
||||
challengeType: 29
|
||||
dashedName: challenge-33
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an input array of seven integers, representing a week's time, where each integer is the amount of hours spent on your phone that day, determine if it is too much screen time based on these constraints:
|
||||
|
||||
- If any single day has 10 hours or more, it's too much.
|
||||
- If the average of any three days in a row is greater than or equal to 8 hours, it’s too much.
|
||||
- If the average of the seven days is greater than or equal to 6 hours, it's too much.
|
||||
|
||||
# --hints--
|
||||
|
||||
`too_much_screen_time([1, 2, 3, 4, 5, 6, 7])` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([1, 2, 3, 4, 5, 6, 7]), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([7, 8, 8, 4, 2, 2, 3])` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([7, 8, 8, 4, 2, 2, 3]), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([5, 6, 6, 6, 6, 6, 6])` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([5, 6, 6, 6, 6, 6, 6]), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([1, 2, 3, 11, 1, 3, 4])` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([1, 2, 3, 11, 1, 3, 4]), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([1, 2, 3, 10, 2, 1, 0])` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([1, 2, 3, 10, 2, 1, 0]), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([3, 3, 5, 8, 8, 9, 4])` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([3, 3, 5, 8, 8, 9, 4]), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`too_much_screen_time([3, 9, 4, 8, 5, 7, 6])` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(too_much_screen_time([3, 9, 4, 8, 5, 7, 6]), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def too_much_screen_time(hours):
|
||||
|
||||
return hours
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def too_much_screen_time(hours):
|
||||
for h in hours:
|
||||
if h >= 10:
|
||||
return True
|
||||
|
||||
for i in range(5):
|
||||
avg3 = sum(hours[i:i+3]) / 3
|
||||
if avg3 >= 8:
|
||||
return True
|
||||
|
||||
weekly_avg = sum(hours) / 7
|
||||
if weekly_avg >= 6:
|
||||
return True
|
||||
|
||||
return False
|
||||
```
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 68af0687ef34c76c28ffa54f
|
||||
title: "Challenge 34: Missing Numbers"
|
||||
challengeType: 29
|
||||
dashedName: challenge-34
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers from 1 to `n`, inclusive, return an array of all the missing integers between 1 and `n` (where `n` is the largest number in the given array).
|
||||
|
||||
- The given array may be unsorted and may contain duplicates.
|
||||
- The returned array should be in ascending order.
|
||||
- If no integers are missing, return an empty array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_missing_numbers([1, 3, 5])` should return `[2, 4]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([1, 3, 5]), [2, 4])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_missing_numbers([1, 2, 3, 4, 5])` should return `[]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([1, 2, 3, 4, 5]), [])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_missing_numbers([1, 10])` should return `[2, 3, 4, 5, 6, 7, 8, 9]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([1, 10]), [2, 3, 4, 5, 6, 7, 8, 9])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_missing_numbers([10, 1, 10, 1, 10, 1])` should return `[2, 3, 4, 5, 6, 7, 8, 9]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([10, 1, 10, 1, 10, 1]), [2, 3, 4, 5, 6, 7, 8, 9])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_missing_numbers([3, 1, 4, 1, 5, 9])` should return `[2, 6, 7, 8]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([3, 1, 4, 1, 5, 9]), [2, 6, 7, 8])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_missing_numbers([1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 6, 8, 9, 3, 2, 10, 7, 4])` should return `[11]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_missing_numbers([1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 6, 8, 9, 3, 2, 10, 7, 4]), [11])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_missing_numbers(arr):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_missing_numbers(arr):
|
||||
if not arr:
|
||||
return []
|
||||
|
||||
n = max(arr)
|
||||
seen = set(arr)
|
||||
result = [i for i in range(1, n + 1) if i not in seen]
|
||||
return result
|
||||
```
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 68b06e589bf227324381476f
|
||||
title: "Challenge 35: Word Frequency"
|
||||
challengeType: 29
|
||||
dashedName: challenge-35
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a paragraph, return an array of the three most frequently occurring words.
|
||||
|
||||
- Words in the paragraph will be separated by spaces.
|
||||
- Ignore case in the given paragraph. For example, treat `Hello` and `hello` as the same word.
|
||||
- Ignore punctuation in the given paragraph. Punctuation consists of commas (`,`), periods (`.`), and exclamation points (`!`).
|
||||
- The returned array should have all lowercase words.
|
||||
- The returned array should be in descending order with the most frequently occurring word first.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_words("Coding in Python is fun because coding Python allows for coding in Python easily while coding")` should return `["coding", "python", "in"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_words("Coding in Python is fun because coding Python allows for coding in Python easily while coding"), ["coding", "python", "in"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_words("I like coding. I like testing. I love debugging!")` should return `["i", "like", "coding"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_words("I like coding. I like testing. I love debugging!"), ["i", "like", "coding"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_words("Debug, test, deploy. Debug, debug, test, deploy. Debug, test, test, deploy!")` should return `["debug", "test", "deploy"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_words("Debug, test, deploy. Debug, debug, test, deploy. Debug, test, test, deploy!"), ["debug", "test", "deploy"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_words(paragraph):
|
||||
|
||||
return paragraph
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def get_words(paragraph):
|
||||
cleaned = re.sub(r'[.,?]', '', paragraph.lower())
|
||||
|
||||
words = cleaned.split()
|
||||
|
||||
freq = {}
|
||||
for word in words:
|
||||
freq[word] = freq.get(word, 0) + 1
|
||||
|
||||
sorted_words = sorted(freq.keys(), key=lambda w: freq[w], reverse=True)
|
||||
|
||||
return sorted_words[:3]
|
||||
```
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 68b06e589bf2273243814771
|
||||
title: "Challenge 36: Thermostat Adjuster"
|
||||
challengeType: 29
|
||||
dashedName: challenge-36
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the current temperature of a room and a target temperature, return a string indicating how to adjust the room temperature based on these constraints:
|
||||
|
||||
- Return `"heat"` if the current temperature is below the target.
|
||||
- Return `"cool"` if the current temperature is above the target.
|
||||
- Return `"hold"` if the current temperature is equal to the target.
|
||||
|
||||
# --hints--
|
||||
|
||||
`adjust_thermostat(68, 72)` should return `"heat"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(68, 72), "heat")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(75, 72)` should return `"cool"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(75, 72), "cool")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(72, 72)` should return `"hold"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(72, 72), "hold")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(-20.5, -10.1)` should return `"heat"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(-20.5, -10.1), "heat")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(100, 99.9)` should return `"cool"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(100, 99.9), "cool")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(0.0, 0.0)` should return `"hold"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(0.0, 0.0), "hold")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def adjust_thermostat(temp, target):
|
||||
|
||||
return temp
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def adjust_thermostat(temp, target):
|
||||
if temp < target:
|
||||
return "heat"
|
||||
elif temp > target:
|
||||
return "cool"
|
||||
else:
|
||||
return "hold"
|
||||
```
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 68b06e589bf2273243814773
|
||||
title: "Challenge 37: Sentence Capitalizer"
|
||||
challengeType: 29
|
||||
dashedName: challenge-37
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a paragraph, return a new paragraph where the first letter of each sentence is capitalized.
|
||||
|
||||
- All other characters should be preserved.
|
||||
- Sentences can end with a period (`.`), one or more question marks (`?`), or one or more exclamation points (`!`).
|
||||
|
||||
# --hints--
|
||||
|
||||
`capitalize("this is a simple sentence.")` should return `"This is a simple sentence."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(capitalize("this is a simple sentence."), "This is a simple sentence.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`capitalize("hello world. how are you?")` should return `"Hello world. How are you?"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(capitalize("hello world. how are you?"), "Hello world. How are you?")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`capitalize("i did today's coding challenge... it was fun!!")` should return `"I did today's coding challenge... It was fun!!"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(capitalize("i did today's coding challenge... it was fun!!"), "I did today's coding challenge... It was fun!!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`capitalize("crazy!!!strange???unconventional...sentences.")` should return `"Crazy!!!Strange???Unconventional...Sentences."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(capitalize("crazy!!!strange???unconventional...sentences."), "Crazy!!!Strange???Unconventional...Sentences.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`capitalize("there's a space before this period . why is there a space before that period ?")` should return `"There's a space before this period . Why is there a space before that period ?"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(capitalize("there's a space before this period . why is there a space before that period ?"), "There's a space before this period . Why is there a space before that period ?")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def capitalize(paragraph):
|
||||
|
||||
return paragraph
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def capitalize(paragraph):
|
||||
def repl(match):
|
||||
return match.group(1) + match.group(2) + match.group(3).upper()
|
||||
|
||||
result = re.sub(r'([.!?]+)(\s*)([a-z])', repl, paragraph)
|
||||
|
||||
if result and result[0].islower():
|
||||
result = result[0].upper() + result[1:]
|
||||
|
||||
return result
|
||||
```
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 68b06e589bf2273243814775
|
||||
title: "Challenge 38: Slug Generator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-38
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return a URL-friendly version of the string using the following constraints:
|
||||
|
||||
- All letters should be lowercase.
|
||||
- All characters that are not letters, numbers, or spaces should be removed.
|
||||
- All spaces should be replaced with the URL-encoded space code `%20`.
|
||||
- Consecutive spaces should be replaced with a single `%20`.
|
||||
- The returned string should not have leading or trailing `%20`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`generate_slug("helloWorld")` should return `"helloworld"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_slug("helloWorld"), "helloworld")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_slug("hello world!")` should return `"hello%20world"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_slug("hello world!"), "hello%20world")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_slug(" hello-world ")` should return `"helloworld"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_slug(" hello-world "), "helloworld")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_slug("hello world")` should return `"hello%20world"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_slug("hello world"), "hello%20world")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_slug(" ?H^3-1*1]0! W[0%R#1]D ")` should return `"h3110%20w0r1d"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_slug(" ?H^3-1*1]0! W[0%R#1]D "), "h3110%20w0r1d")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def generate_slug(str):
|
||||
|
||||
return str
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def generate_slug(s):
|
||||
cleaned = re.sub(r'[^a-zA-Z0-9 ]+', '', s)
|
||||
cleaned = re.sub(r'\s+', ' ', cleaned).strip()
|
||||
return cleaned.lower().replace(' ', '%20')
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 68b06e589bf2273243814777
|
||||
title: "Challenge 39: Fill The Tank"
|
||||
challengeType: 29
|
||||
dashedName: challenge-39
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the size of a fuel tank, the current fuel level, and the price per gallon, return the cost to fill the tank all the way.
|
||||
|
||||
- `tankSize` is the total capacity of the tank in gallons.
|
||||
- `fuelLevel` is the current amount of fuel in the tank in gallons.
|
||||
- `pricePerGallon` is the cost of one gallon of fuel.
|
||||
- The returned value should be rounded to two decimal places in the format: `"$d.dd"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cost_to_fill(20, 0, 4.00)` should return `"$80.00"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(cost_to_fill(20, 0, 4.00), "$80.00")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`cost_to_fill(15, 10, 3.50)` should return `"$17.50"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(cost_to_fill(15, 10, 3.50), "$17.50")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`cost_to_fill(18, 9, 3.25)` should return `"$29.25"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(cost_to_fill(18, 9, 3.25), "$29.25")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`cost_to_fill(12, 12, 4.99)` should return `"$0.00"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(cost_to_fill(12, 12, 4.99), "$0.00")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`cost_to_fill(15, 9.5, 3.98)` should return `"$21.89"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(cost_to_fill(15, 9.5, 3.98), "$21.89")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def cost_to_fill(tank_size, fuel_level, price_per_gallon):
|
||||
|
||||
return tank_size
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def cost_to_fill(tank_size, fuel_level, price_per_gallon):
|
||||
gallons_needed = tank_size - fuel_level
|
||||
cost = gallons_needed * price_per_gallon
|
||||
return f"${cost:.2f}"
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 68b1f72371a5ac895ac70a02
|
||||
title: "Challenge 40: Photo Storage"
|
||||
challengeType: 29
|
||||
dashedName: challenge-40
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a photo size in megabytes (MB), and hard drive capacity in gigabytes (GB), return the number of photos the hard drive can store using the following constraints:
|
||||
|
||||
- 1 gigabyte equals 1000 megabytes.
|
||||
- Return the number of whole photos the drive can store.
|
||||
|
||||
# --hints--
|
||||
|
||||
`number_of_photos(1, 1)` should return `1000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_photos(1, 1), 1000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_photos(2, 1)` should return `500`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_photos(2, 1), 500)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_photos(4, 256)` should return `64000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_photos(4, 256), 64000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_photos(3.5, 750)` should return `214285`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_photos(3.5, 750), 214285)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_photos(3.5, 5.5)` should return `1571`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_photos(3.5, 5.5), 1571)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def number_of_photos(photo_size_mb, drive_size_gb):
|
||||
|
||||
return photo_size_mb
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def number_of_photos(photo_size_mb, drive_size_gb):
|
||||
drive_size_mb = drive_size_gb * 1000
|
||||
return drive_size_mb // photo_size_mb
|
||||
```
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 68b1f72371a5ac895ac70a04
|
||||
title: "Challenge 41: File Storage"
|
||||
challengeType: 29
|
||||
dashedName: challenge-41
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a file size, a unit for the file size, and hard drive capacity in gigabytes (GB), return the number of files the hard drive can store using the following constraints:
|
||||
|
||||
- The unit for the file size can be bytes (`"B"`), kilobytes (`"KB"`), or megabytes (`"MB"`).
|
||||
- Return the number of whole files the drive can fit.
|
||||
- Use the following conversions:
|
||||
|
||||
| Unit | Equivalent |
|
||||
|:----:|:----------:|
|
||||
| 1 B | 1 B |
|
||||
| 1 KB | 1000 B |
|
||||
| 1 MB | 1000 KB |
|
||||
| 1 GB | 1000 MB |
|
||||
|
||||
For example, given `500`, `"KB"`, and `1` as arguments, determine how many 500 KB files can fit on a 1 GB hard drive.
|
||||
|
||||
# --hints--
|
||||
|
||||
`number_of_files(500, "KB", 1)` should return `2000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(500, "KB", 1), 2000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_files(50000, "B", 1)` should return `20000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(50000, "B", 1), 20000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_files(5, "MB", 1)` should return `200`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(5, "MB", 1), 200)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_files(4096, "B", 1.5)` should return `366210`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(4096, "B", 1.5), 366210)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_files(220.5, "KB", 100)` should return `453514`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(220.5, "KB", 100), 453514)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_files(4.5, "MB", 750)` should return `166666`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_files(4.5, "MB", 750), 166666)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def number_of_files(file_size, file_unit, drive_size_gb):
|
||||
|
||||
return file_size
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def number_of_files(file_size, file_unit, drive_size_gb):
|
||||
drive_size_bytes = drive_size_gb * 1000 * 1000 * 1000
|
||||
|
||||
if file_unit == "B":
|
||||
file_size_bytes = file_size
|
||||
elif file_unit == "KB":
|
||||
file_size_bytes = file_size * 1000
|
||||
else:
|
||||
file_size_bytes = file_size * 1000 * 1000
|
||||
|
||||
return int(drive_size_bytes // file_size_bytes)
|
||||
```
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
---
|
||||
id: 68b1f72371a5ac895ac70a06
|
||||
title: "Challenge 42: Video Storage"
|
||||
challengeType: 29
|
||||
dashedName: challenge-42
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a video size, a unit for the video size, a hard drive capacity, and a unit for the hard drive, return the number of videos the hard drive can store using the following constraints:
|
||||
|
||||
- The unit for the video size can be bytes (`"B"`), kilobytes (`"KB"`), megabytes (`"MB"`), or gigabytes (`"GB"`).
|
||||
- If not given one of the video units above, return `"Invalid video unit"`.
|
||||
- The unit of the hard drive capacity can be gigabytes (`"GB"`) or terabytes (`"TB"`).
|
||||
- If not given one of the hard drive units above, return `"Invalid drive unit"`.
|
||||
- Return the number of whole videos the drive can fit.
|
||||
- Use the following conversions:
|
||||
|
||||
| Unit | Equivalent |
|
||||
|:----:|:----------:|
|
||||
| 1 B | 1 B |
|
||||
| 1 KB | 1000 B |
|
||||
| 1 MB | 1000 KB |
|
||||
| 1 GB | 1000 MB |
|
||||
| 1 TB | 1000 GB |
|
||||
|
||||
For example, given `500`, `"MB"`, `100`, and `"GB"` as arguments, determine how many 500 MB videos can fit on a 100 GB hard drive.
|
||||
|
||||
# --hints--
|
||||
|
||||
`number_of_videos(500, "MB", 100, "GB")` should return `200`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_videos(500, "MB", 100, "GB"), 200)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_videos(1, "TB", 10, "TB")` should return `"Invalid video unit"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_videos(1, "TB", 10, "TB"), "Invalid video unit")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_videos(2000, "MB", 100000, "MB")` should return `"Invalid drive unit"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_videos(2000, "MB", 100000, "MB"), "Invalid drive unit")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_videos(500000, "KB", 2, "TB")` should return `4000`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_videos(500000, "KB", 2, "TB"), 4000)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`number_of_videos(1.5, "GB", 2.2, "TB")` should return `1466`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(number_of_videos(1.5, "GB", 2.2, "TB"), 1466)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def number_of_videos(video_size, video_unit, drive_size, drive_unit):
|
||||
|
||||
return video_size
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def number_of_videos(video_size, video_unit, drive_size, drive_unit):
|
||||
video_units = {"KB": 1000, "MB": 1000 * 1000, "GB": 1000 * 1000 * 1000}
|
||||
drive_units = {"GB": 1000 * 1000 * 1000, "TB": 1000 * 1000 * 1000 * 1000}
|
||||
|
||||
if video_unit not in video_units:
|
||||
return "Invalid video unit"
|
||||
if drive_unit not in drive_units:
|
||||
return "Invalid drive unit"
|
||||
|
||||
video_bytes = video_size * video_units[video_unit]
|
||||
drive_bytes = drive_size * drive_units[drive_unit]
|
||||
|
||||
return int(drive_bytes // video_bytes)
|
||||
```
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 68b1f72371a5ac895ac70a08
|
||||
title: "Challenge 43: Digits vs Letters"
|
||||
challengeType: 29
|
||||
dashedName: challenge-43
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return `"digits"` if the string has more digits than letters, `"letters"` if it has more letters than digits, and `"tie"` if it has the same amount of digits and letters.
|
||||
|
||||
- Digits consist of `0-9`.
|
||||
- Letters consist of `a-z` in upper or lower case.
|
||||
- Ignore any other characters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`digits_or_letters("abc123")` should return `"tie"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("abc123"), "tie")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`digits_or_letters("a1b2c3d")` should return `"letters"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("a1b2c3d"), "letters")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`digits_or_letters("1a2b3c4")` should return `"digits"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("1a2b3c4"), "digits")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`digits_or_letters("abc123!@#DEF")` should return `"letters"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("abc123!@#DEF"), "letters")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`digits_or_letters("H3110 W0R1D")` should return `"digits"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("H3110 W0R1D"), "digits")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`digits_or_letters("P455W0RD")` should return `"tie"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(digits_or_letters("P455W0RD"), "tie")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def digits_or_letters(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def digits_or_letters(s):
|
||||
digit_count = 0
|
||||
letter_count = 0
|
||||
|
||||
for char in s:
|
||||
if char.isdigit():
|
||||
digit_count += 1
|
||||
elif char.isalpha():
|
||||
letter_count += 1
|
||||
|
||||
if digit_count > letter_count:
|
||||
return "digits"
|
||||
if letter_count > digit_count:
|
||||
return "letters"
|
||||
return "tie"
|
||||
```
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 68b1f72371a5ac895ac70a0a
|
||||
title: "Challenge 44: String Mirror"
|
||||
challengeType: 29
|
||||
dashedName: challenge-44
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two strings, determine if the second string is a mirror of the first.
|
||||
|
||||
- A string is considered a mirror if it contains the same letters in reverse order.
|
||||
- Treat uppercase and lowercase letters as distinct.
|
||||
- Ignore all non-alphabetical characters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_mirror("helloworld", "helloworld")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("helloworld", "helloworld"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("Hello World", "dlroW olleH")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("Hello World", "dlroW olleH"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("RaceCar", "raCecaR")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("RaceCar", "raCecaR"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("RaceCar", "RaceCar")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("RaceCar", "RaceCar"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("Mirror", "rorrim")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("Mirror", "rorrim"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("Hello World", "dlroW-olleH")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("Hello World", "dlroW-olleH"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_mirror("Hello World", "!dlroW !olleH")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_mirror("Hello World", "!dlroW !olleH"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_mirror(str1, str2):
|
||||
|
||||
return str1
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_mirror(str1, str2):
|
||||
clean1 = "".join(c for c in str1 if c.isalpha())
|
||||
clean2 = "".join(c for c in str2 if c.isalpha())
|
||||
return clean1[::-1] == clean2
|
||||
```
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
---
|
||||
id: 68b7687dded630607aceccab
|
||||
title: "Challenge 45: Perfect Square"
|
||||
challengeType: 29
|
||||
dashedName: challenge-45
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer, determine if it is a perfect square.
|
||||
|
||||
- A number is a perfect square if you can multiply an integer by itself to achieve the number. For example, 9 is a perfect square because you can multiply 3 by itself to get it.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_perfect_square(9)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(9), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(49)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(49), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(1)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(1), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(2)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(2), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(99)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(99), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(-9)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(-9), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(0)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(0), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_perfect_square(25281)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_perfect_square(25281), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_perfect_square(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import math
|
||||
def is_perfect_square(n):
|
||||
if n < 0:
|
||||
return False
|
||||
root = int(math.sqrt(n))
|
||||
return root * root == n
|
||||
```
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 68b7687dded630607aceccad
|
||||
title: "Challenge 46: 2nd Largest"
|
||||
challengeType: 29
|
||||
dashedName: challenge-46
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array, return the second largest distinct number.
|
||||
|
||||
# --hints--
|
||||
|
||||
`second_largest([1, 2, 3, 4])` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(second_largest([1, 2, 3, 4]), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`second_largest([20, 139, 94, 67, 31])` should return `94`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(second_largest([20, 139, 94, 67, 31]), 94)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`second_largest([2, 3, 4, 6, 6])` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(second_largest([2, 3, 4, 6, 6]), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`second_largest([10, -17, 55.5, 44, 91, 0])` should return `55.5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(second_largest([10, -17, 55.5, 44, 91, 0]), 55.5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`second_largest([1, 0, -1, 0, 1, 0, -1, 1, 0])` should return `0`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(second_largest([1, 0, -1, 0, 1, 0, -1, 1, 0]), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def second_largest(arr):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def second_largest(arr):
|
||||
unique = list(set(arr))
|
||||
unique.sort(reverse=True)
|
||||
return unique[1]
|
||||
```
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 68b7687dded630607aceccaf
|
||||
title: "Challenge 47: Caught Speeding"
|
||||
challengeType: 29
|
||||
dashedName: challenge-47
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of numbers representing the speed at which vehicles were observed traveling, and a number representing the speed limit, return an array with two items, the number of vehicles that were speeding, followed by the average amount beyond the speed limit of those vehicles.
|
||||
|
||||
- If there were no vehicles speeding, return `[0, 0]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`speeding([50, 60, 55], 60)` should return `[0, 0]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(speeding([50, 60, 55], 60), [0, 0])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`speeding([58, 50, 60, 55], 55)` should return `[2, 4]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(speeding([58, 50, 60, 55], 55), [2, 4])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`speeding([61, 81, 74, 88, 65, 71, 68], 70)` should return `[4, 8.5]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(speeding([61, 81, 74, 88, 65, 71, 68], 70), [4, 8.5])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`speeding([100, 105, 95, 102], 100)` should return `[2, 3.5]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(speeding([100, 105, 95, 102], 100), [2, 3.5])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`speeding([40, 45, 44, 50, 112, 39], 55)` should return `[1, 57]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(speeding([40, 45, 44, 50, 112, 39], 55), [1, 57])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def speeding(speeds, limit):
|
||||
|
||||
return speeds
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def speeding(speeds, limit):
|
||||
speeding = 0
|
||||
total_over = 0
|
||||
|
||||
for speed in speeds:
|
||||
if speed > limit:
|
||||
speeding += 1
|
||||
total_over += (speed - limit)
|
||||
|
||||
if speeding == 0:
|
||||
return [0, 0]
|
||||
|
||||
return [speeding, total_over / speeding]
|
||||
```
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
---
|
||||
id: 68b7687dded630607aceccb1
|
||||
title: "Challenge 48: Spam Detector"
|
||||
challengeType: 29
|
||||
dashedName: challenge-48
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a phone number in the format `"+A (BBB) CCC-DDDD"`, where each letter represents a digit as follows:
|
||||
|
||||
- `A` represents the country code and can be any number of digits.
|
||||
- `BBB` represents the area code and will always be three digits.
|
||||
- `CCC` and `DDDD` represent the local number and will always be three and four digits long, respectively.
|
||||
|
||||
Determine if it's a spam number based on the following criteria:
|
||||
|
||||
- The country code is greater than 2 digits long or doesn't begin with a zero (`0`).
|
||||
- The area code is greater than 900 or less than 200.
|
||||
- The sum of first three digits of the local number appears within last four digits of the local number.
|
||||
- The number has the same digit four or more times in a row (ignoring the formatting characters).
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_spam("+0 (200) 234-0182")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+0 (200) 234-0182"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+091 (555) 309-1922")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+091 (555) 309-1922"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+1 (555) 435-4792")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+1 (555) 435-4792"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+0 (955) 234-4364")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+0 (955) 234-4364"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+0 (155) 131-6943")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+0 (155) 131-6943"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+0 (555) 135-0192")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+0 (555) 135-0192"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+0 (555) 564-1987")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+0 (555) 564-1987"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_spam("+00 (555) 234-0182")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_spam("+00 (555) 234-0182"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_spam(number):
|
||||
|
||||
return number
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def is_spam(number):
|
||||
digits = re.sub(r"\D", "", number)
|
||||
match = re.match(r"^\+(\d+)\s\((\d{3})\)\s(\d{3})-(\d{4})$", number)
|
||||
country_code, area_code, ccc, dddd = match.groups()
|
||||
|
||||
if len(country_code) > 2 or not country_code.startswith("0"):
|
||||
return True
|
||||
|
||||
area_num = int(area_code)
|
||||
if area_num > 900 or area_num < 200:
|
||||
return True
|
||||
|
||||
sum_ccc = sum(int(d) for d in ccc)
|
||||
if str(sum_ccc) in dddd:
|
||||
return True
|
||||
|
||||
if re.search(r"(\d)\1\1\1", digits):
|
||||
return True
|
||||
|
||||
return False
|
||||
```
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 68b7687dded630607aceccb3
|
||||
title: "Challenge 49: CSV Header Parser"
|
||||
challengeType: 29
|
||||
dashedName: challenge-49
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the first line of a comma-separated values (CSV) file, return an array containing the headings.
|
||||
|
||||
- The first line of a CSV file contains headings separated by commas.
|
||||
- Remove any leading or trailing whitespace from each heading.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_headings("name,age,city")` should return `["name", "age", "city"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_headings("name,age,city"), ["name", "age", "city"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_headings("first name,last name,phone")` should return `["first name", "last name", "phone"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_headings("first name,last name,phone"), ["first name", "last name", "phone"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_headings("username , email , signup date ")` should return `["username", "email", "signup date"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_headings("username , email , signup date "), ["username", "email", "signup date"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_headings(csv):
|
||||
|
||||
return csv
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_headings(csv):
|
||||
return [h.strip() for h in csv.split(",")]
|
||||
```
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 68b7cadffed0e75a517da66f
|
||||
title: "Challenge 50: Longest Word"
|
||||
challengeType: 29
|
||||
dashedName: challenge-50
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a sentence, return the longest word in the sentence.
|
||||
|
||||
- Ignore periods (`.`) when determining word length.
|
||||
- If multiple words are ties for the longest, return the first one that occurs.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_longest_word("coding is fun")` should return `"coding"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_longest_word("coding is fun"), "coding")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_longest_word("Coding challenges are fun and educational.")` should return `"educational"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_longest_word("Coding challenges are fun and educational."), "educational")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_longest_word("This sentence has multiple long words.")` should return `"sentence"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_longest_word("This sentence has multiple long words."), "sentence")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_longest_word(sentence):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_longest_word(sentence):
|
||||
words = sentence.split()
|
||||
longest = ''
|
||||
for word in words:
|
||||
clean_word = word.replace('.', '')
|
||||
if len(clean_word) > len(longest):
|
||||
longest = clean_word
|
||||
return longest
|
||||
```
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: 68b7cadffed0e75a517da671
|
||||
title: "Challenge 51: Phone Number Formatter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-51
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of eleven digits, return the string as a phone number in this format: `"+D (DDD) DDD-DDDD"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`format_number("05552340182")` should return `"+0 (555) 234-0182"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format_number("05552340182"), "+0 (555) 234-0182")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`format_number("15554354792")` should return `"+1 (555) 435-4792"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format_number("15554354792"), "+1 (555) 435-4792")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def format_number(number):
|
||||
|
||||
return number
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def format_number(number):
|
||||
country = number[0]
|
||||
area = number[1:4]
|
||||
prefix = number[4:7]
|
||||
line = number[7:]
|
||||
|
||||
return f"+{country} ({area}) {prefix}-{line}"
|
||||
```
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 68b7cadffed0e75a517da673
|
||||
title: "Challenge 52: Binary to Decimal"
|
||||
challengeType: 29
|
||||
dashedName: challenge-52
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a binary number, return its decimal equivalent as a number.
|
||||
|
||||
A binary number uses only the digits `0` and `1` to represent any number. To convert binary to decimal, multiply each digit by a power of `2` and add them together. Start by multiplying the rightmost digit by `2^0`, the next digit to the left by `2^1`, and so on. Once all digits have been multiplied by a power of `2`, add the result together.
|
||||
|
||||
For example, the binary number `101` equals `5` in decimal because:
|
||||
|
||||
```mathml
|
||||
1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`to_decimal("101")` should return `5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_decimal("101"), 5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_decimal("1010")` should return `10`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_decimal("1010"), 10)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_decimal("10010")` should return `18`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_decimal("10010"), 18)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_decimal("1010101")` should return `85`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_decimal("1010101"), 85)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def to_decimal(binary):
|
||||
|
||||
return binary
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def to_decimal(binary):
|
||||
decimal = 0
|
||||
for i, bit_char in enumerate(reversed(binary)):
|
||||
bit = int(bit_char)
|
||||
decimal += bit * (2 ** i)
|
||||
return decimal
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 68b7cadffed0e75a517da675
|
||||
title: "Challenge 53: Decimal to Binary"
|
||||
challengeType: 29
|
||||
dashedName: challenge-53
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a non-negative integer, return its binary representation as a string.
|
||||
|
||||
A binary number uses only the digits `0` and `1` to represent any number. To convert a decimal number to binary, repeatedly divide the number by `2` and record the remainder. Repeat until the number is zero. Read the remainders last recorded to first. For example, to convert `12` to binary:
|
||||
|
||||
```mathml
|
||||
12 ÷ 2 = 6 remainder 0
|
||||
6 ÷ 2 = 3 remainder 0
|
||||
3 ÷ 2 = 1 remainder 1
|
||||
1 ÷ 2 = 0 remainder 1
|
||||
```
|
||||
|
||||
`12` in binary is `1100`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`to_binary(5)` should return `"101"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_binary(5), "101")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_binary(12)` should return `"1100"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_binary(12), "1100")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_binary(50)` should return `"110010"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_binary(50), "110010")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_binary(99)` should return `"1100011"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_binary(99), "1100011")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def to_binary(decimal):
|
||||
|
||||
return decimal
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def to_binary(decimal):
|
||||
if decimal == 0:
|
||||
return "0"
|
||||
binary = ""
|
||||
while decimal > 0:
|
||||
binary = str(decimal % 2) + binary
|
||||
decimal //= 2
|
||||
return binary
|
||||
```
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
---
|
||||
id: 68b7cadffed0e75a517da677
|
||||
title: "Challenge 54: P@ssw0rd Str3ngth!"
|
||||
challengeType: 29
|
||||
dashedName: challenge-54
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a password string, return `"weak"`, `"medium"`, or `"strong"` based on the strength of the password.
|
||||
|
||||
A password is evaluated according to the following rules:
|
||||
|
||||
- It is at least 8 characters long.
|
||||
- It contains both uppercase and lowercase letters.
|
||||
- It contains at least one number.
|
||||
- It contains at least one special character from this set: `!`, `@`, `#`, `$`, `%`, `^`, `&`, or `*`.
|
||||
|
||||
Return `"weak"` if the password meets fewer than two of the rules.
|
||||
Return `"medium"` if the password meets 2 or 3 of the rules.
|
||||
Return `"strong"` if the password meets all 4 rules.
|
||||
|
||||
# --hints--
|
||||
|
||||
`check_strength("123456")` should return `"weak"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("123456"), "weak")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("pass!!!")` should return `"weak"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("pass!!!"), "weak")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("Qwerty")` should return `"weak"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("Qwerty"), "weak")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("PASSWORD")` should return `"weak"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("PASSWORD"), "weak")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("PASSWORD!")` should return `"medium"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("PASSWORD!"), "medium")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("PassWord%^!")` should return `"medium"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("PassWord%^!"), "medium")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("qwerty12345")` should return `"medium"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("qwerty12345"), "medium")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("S3cur3P@ssw0rd")` should return `"strong"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("S3cur3P@ssw0rd"), "strong")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`check_strength("C0d3&Fun!")` should return `"strong"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(check_strength("C0d3&Fun!"), "strong")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def check_strength(password):
|
||||
|
||||
return password
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def check_strength(password):
|
||||
rules_met = 0
|
||||
|
||||
if len(password) >= 8:
|
||||
rules_met += 1
|
||||
if re.search(r'[a-z]', password) and re.search(r'[A-Z]', password):
|
||||
rules_met += 1
|
||||
if re.search(r'\d', password):
|
||||
rules_met += 1
|
||||
if re.search(r'[!@#$%^&*]', password):
|
||||
rules_met += 1
|
||||
|
||||
if rules_met < 2:
|
||||
return "weak"
|
||||
elif rules_met < 4:
|
||||
return "medium"
|
||||
else:
|
||||
return "strong"
|
||||
```
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
---
|
||||
id: 68c1a929005bf54d342aa8d2
|
||||
title: "Challenge 55: Space Week Day 1: Stellar Classification"
|
||||
challengeType: 29
|
||||
dashedName: challenge-55
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
October 4th marks the beginning of World Space Week. The next seven days will bring you astronomy-themed coding challenges.
|
||||
|
||||
For today's challenge, you are given the surface temperature of a star in Kelvin (K) and need to determine its stellar classification based on the following ranges:
|
||||
|
||||
- `"O"`: 30,000 K or higher
|
||||
- `"B"`: 10,000 K - 29,999 K
|
||||
- `"A"`: 7,500 K - 9,999 K
|
||||
- `"F"`: 6,000 K - 7,499 K
|
||||
- `"G"`: 5,200 K - 5,999 K
|
||||
- `"K"`: 3,700 K - 5,199 K
|
||||
- `"M"`: 0 K - 3,699 K
|
||||
|
||||
- Return the classification of the given star.
|
||||
|
||||
# --hints--
|
||||
|
||||
`classification(5778)` should return `"G"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(5778), "G")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(2400)` should return `"M"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(2400), "M")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(9999)` should return `"A"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(9999), "A")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(3700)` should return `"K"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(3700), "K")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(3699)` should return `"M"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(3699), "M")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(210000)` should return `"O"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(210000), "O")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(6000)` should return `"F"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(6000), "F")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`classification(11432)` should return `"B"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(classification(11432), "B")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def classification(temp):
|
||||
|
||||
return temp
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def classification(temp):
|
||||
if temp >= 30000:
|
||||
return "O"
|
||||
elif temp >= 10000:
|
||||
return "B"
|
||||
elif temp >= 7500:
|
||||
return "A"
|
||||
elif temp >= 6000:
|
||||
return "F"
|
||||
elif temp >= 5200:
|
||||
return "G"
|
||||
elif temp >= 3700:
|
||||
return "K"
|
||||
else:
|
||||
return "M"
|
||||
```
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 68c1a929005bf54d342aa8d3
|
||||
title: "Challenge 56: Space Week Day 2: Exoplanet Search"
|
||||
challengeType: 29
|
||||
dashedName: challenge-56
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For the second day of Space Week, you are given a string where each character represents the luminosity reading of a star. Determine if the readings have detected an exoplanet using the transit method. The transit method is when a planet passes in front of a star, reducing its observed luminosity.
|
||||
|
||||
- Luminosity readings only comprise of characters `0-9` and `A-Z` where each reading corresponds to the following numerical values:
|
||||
- Characters `0-9` correspond to luminosity levels `0-9`.
|
||||
- Characters `A-Z` correspond to luminosity levels `10-35`.
|
||||
|
||||
A star is considered to have an exoplanet if any single reading is less than or equal to 80% of the average of all readings. For example, if the average luminosity of a star is 10, it would be considered to have a exoplanet if any single reading is 8 or less.
|
||||
|
||||
# --hints--
|
||||
|
||||
`has_exoplanet("665544554")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("665544554"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`has_exoplanet("FGFFCFFGG")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("FGFFCFFGG"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`has_exoplanet("MONOPLONOMONPLNOMPNOMP")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("MONOPLONOMONPLNOMPNOMP"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`has_exoplanet("FREECODECAMP")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("FREECODECAMP"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`has_exoplanet("9AB98AB9BC98A")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("9AB98AB9BC98A"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`has_exoplanet("ZXXWYZXYWYXZEGZXWYZXYGEE")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(has_exoplanet("ZXXWYZXYWYXZEGZXWYZXYGEE"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def has_exoplanet(readings):
|
||||
|
||||
return readings
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def has_exoplanet(readings):
|
||||
values = [int(c, 36) for c in readings]
|
||||
average = sum(values) / len(values)
|
||||
threshold = average * 0.8
|
||||
for v in values:
|
||||
if v <= threshold:
|
||||
return True
|
||||
return False
|
||||
```
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
---
|
||||
id: 68c1a929005bf54d342aa8d4
|
||||
title: "Challenge 57: Space Week Day 3: Phone Home"
|
||||
challengeType: 29
|
||||
dashedName: challenge-57
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For day three of Space Week, you are given an array of numbers representing distances (in kilometers) between yourself, satellites, and your home planet in a communication route. Determine how long it will take a message sent through the route to reach its destination planet using the following constraints:
|
||||
|
||||
- The first value in the array is the distance from your location to the first satellite.
|
||||
- Each subsequent value, except for the last, is the distance to the next satellite.
|
||||
- The last value in the array is the distance from the previous satellite to your home planet.
|
||||
- The message travels at 300,000 km/s.
|
||||
- Each satellite the message **passes through** adds a 0.5 second transmission delay.
|
||||
- Return a number rounded to 4 decimal places, with trailing zeros removed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`send_message([300000, 300000])` should return `2.5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([300000, 300000]), 2.5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`send_message([384400, 384400])` should return `3.0627`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([384400, 384400]), 3.0627)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`send_message([54600000, 54600000])` should return `364.5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([54600000, 54600000]), 364.5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`send_message([1000000, 500000000, 1000000])` should return `1674.3333`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([1000000, 500000000, 1000000]), 1674.3333)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`send_message([10000, 21339, 50000, 31243, 10000])` should return `2.4086`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([10000, 21339, 50000, 31243, 10000]), 2.4086)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`send_message([802101, 725994, 112808, 3625770, 481239])` should return `21.1597`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(send_message([802101, 725994, 112808, 3625770, 481239]), 21.1597)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def send_message(route):
|
||||
|
||||
return route
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def send_message(route):
|
||||
total_distance = sum(route)
|
||||
delay = (len(route) - 1) * 0.5
|
||||
time = total_distance / 300_000
|
||||
total = time + delay
|
||||
return round(total, 4)
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
id: 68c1a929005bf54d342aa8d5
|
||||
title: "Challenge 58: Space Week Day 4: Landing Spot"
|
||||
challengeType: 29
|
||||
dashedName: challenge-58
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In day four of Space Week, you are given a matrix of numbers (an array of arrays), representing potential landing spots for your rover. Find the safest landing spot based on the following rules:
|
||||
|
||||
- Each spot in the matrix will contain a number from `0-9`, inclusive.
|
||||
- Any `0` represents a potential landing spot.
|
||||
- Any number other than `0` is too dangerous to land. The higher the number, the more dangerous.
|
||||
- The safest spot is defined as the `0` cell whose surrounding cells (up to 4 neighbors, ignore diagonals) have the lowest total danger.
|
||||
- Ignore out-of-bounds neighbors (corners and edges just have fewer neighbors).
|
||||
- Return the indices of the safest landing spot. There will always only be one safest spot.
|
||||
|
||||
For instance, given:
|
||||
|
||||
```js
|
||||
[
|
||||
[1, 0],
|
||||
[2, 0]
|
||||
]
|
||||
```
|
||||
|
||||
Return `[0, 1]`, the indices for the `0` in the first array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_landing_spot([[1, 0], [2, 0]])` should return `[0, 1]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_landing_spot([[1, 0], [2, 0]]), [0, 1])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_landing_spot([[9, 0, 3], [7, 0, 4], [8, 0, 5]])` should return `[1, 1]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_landing_spot([[9, 0, 3], [7, 0, 4], [8, 0, 5]]), [1, 1])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_landing_spot([[1, 2, 1], [0, 0, 2], [3, 0, 0]])` should return `[2, 2]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_landing_spot([[1, 2, 1], [0, 0, 2], [3, 0, 0]]), [2, 2])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_landing_spot([[9, 6, 0, 8], [7, 1, 1, 0], [3, 0, 3, 9], [8, 6, 0, 9]])` should return `[2, 1]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_landing_spot([[9, 6, 0, 8], [7, 1, 1, 0], [3, 0, 3, 9], [8, 6, 0, 9]]), [2, 1])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_landing_spot(matrix):
|
||||
|
||||
return matrix
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_landing_spot(matrix):
|
||||
best_spot = None
|
||||
lowest_neighbor_sum = float('inf')
|
||||
|
||||
for i in range(len(matrix)):
|
||||
for j in range(len(matrix[i])):
|
||||
if matrix[i][j] == 0:
|
||||
current_neighbor_sum = 0
|
||||
|
||||
if i > 0:
|
||||
current_neighbor_sum += matrix[i - 1][j]
|
||||
if j < len(matrix[i]) - 1:
|
||||
current_neighbor_sum += matrix[i][j + 1]
|
||||
if i < len(matrix) - 1:
|
||||
current_neighbor_sum += matrix[i + 1][j]
|
||||
if j > 0:
|
||||
current_neighbor_sum += matrix[i][j - 1]
|
||||
|
||||
if current_neighbor_sum < lowest_neighbor_sum:
|
||||
lowest_neighbor_sum = current_neighbor_sum
|
||||
best_spot = [i, j]
|
||||
|
||||
return best_spot
|
||||
```
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 68c1a929005bf54d342aa8d6
|
||||
title: "Challenge 59: Space Week Day 5: Goldilocks Zone"
|
||||
challengeType: 29
|
||||
dashedName: challenge-59
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For the fifth day of Space Week, you will calculate the "Goldilocks zone" of a star - the region around a star where conditions are "just right" for liquid water to exist.
|
||||
|
||||
Given the mass of a star, return an array with the start and end distances of its Goldilocks Zone in Astronomical Units.
|
||||
|
||||
To calculate the Goldilocks Zone:
|
||||
|
||||
1. Find the luminosity of the star by raising its mass to the power of 3.5.
|
||||
2. The start of the zone is 0.95 times the square root of its luminosity.
|
||||
3. The end of the zone is 1.37 times the square root of its luminosity.
|
||||
|
||||
- Return the distances rounded to two decimal places.
|
||||
|
||||
For example, given `1` as a mass, return `[0.95, 1.37]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`goldilocks_zone(1)` should return `[0.95, 1.37]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(goldilocks_zone(1), [0.95, 1.37])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`goldilocks_zone(0.5)` should return `[0.28, 0.41]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(goldilocks_zone(0.5), [0.28, 0.41])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`goldilocks_zone(6)` should return `[21.85, 31.51]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(goldilocks_zone(6), [21.85, 31.51])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`goldilocks_zone(3.7)` should return `[9.38, 13.52]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(goldilocks_zone(3.7), [9.38, 13.52])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`goldilocks_zone(20)` should return `[179.69, 259.13]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(goldilocks_zone(20), [179.69, 259.13])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def goldilocks_zone(mass):
|
||||
|
||||
return mass
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import math
|
||||
def goldilocks_zone(mass):
|
||||
luminosity = mass ** 3.5
|
||||
start = 0.95 * math.sqrt(luminosity)
|
||||
end = 1.37 * math.sqrt(luminosity)
|
||||
return [round(start, 2), round(end, 2)]
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 68c497f3aaefc9fd9f1b0e24
|
||||
title: "Challenge 60: Space Week Day 6: Moon Phase"
|
||||
challengeType: 29
|
||||
dashedName: challenge-60
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For day six of Space Week, you will be given a date in the format `"YYYY-MM-DD"` and need to determine the phase of the moon for that day using the following rules:
|
||||
|
||||
Use a simplified lunar cycle of 28 days, divided into four equal phases:
|
||||
|
||||
- `"New"`: days 1 - 7
|
||||
- `"Waxing"`: days 8 - 14
|
||||
- `"Full"`: days 15 - 21
|
||||
- `"Waning"`: days 22 - 28
|
||||
|
||||
After day 28, the cycle repeats with day 1, a new moon.
|
||||
|
||||
- Use `"2000-01-06"` as a reference new moon (day 1 of the cycle) to determine the phase of the given day.
|
||||
- You will not be given any dates before the reference date.
|
||||
- Return the correct phase as a string.
|
||||
|
||||
**Note:** Day 1 represents the day of the new moon, meaning 0 days have passed since the last new moon.
|
||||
|
||||
# --hints--
|
||||
|
||||
`moon_phase("2000-01-12")` should return `"New"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(moon_phase("2000-01-12"), "New")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`moon_phase("2000-01-13")` should return `"Waxing"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(moon_phase("2000-01-13"), "Waxing")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`moon_phase("2014-10-15")` should return `"Full"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(moon_phase("2014-10-15"), "Full")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`moon_phase("2012-10-21")` should return `"Waning"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(moon_phase("2012-10-21"), "Waning")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`moon_phase("2022-12-14")` should return `"New"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(moon_phase("2022-12-14"), "New")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def moon_phase(date_string):
|
||||
|
||||
return date_string
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
from datetime import datetime
|
||||
def moon_phase(date_string):
|
||||
ref_date = datetime.strptime("2000-01-06", "%Y-%m-%d")
|
||||
target_date = datetime.strptime(date_string, "%Y-%m-%d")
|
||||
|
||||
diff_days = (target_date - ref_date).days
|
||||
cycle_day = (diff_days % 28) + 1
|
||||
|
||||
if cycle_day <= 7:
|
||||
return "New"
|
||||
elif cycle_day <= 14:
|
||||
return "Waxing"
|
||||
elif cycle_day <= 21:
|
||||
return "Full"
|
||||
else:
|
||||
return "Waning"
|
||||
```
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 68c497f3aaefc9fd9f1b0e25
|
||||
title: "Challenge 61: Space Week Day 7: Launch Fuel"
|
||||
challengeType: 29
|
||||
dashedName: challenge-61
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For the final day of Space Week, you will be given the mass in kilograms (kg) of a payload you want to send to orbit. Determine the amount of fuel needed to send your payload to orbit using the following rules:
|
||||
|
||||
- Rockets require 1 kg of fuel per 5 kg of mass they must lift.
|
||||
- Fuel itself has mass. So when you add fuel, the mass to lift goes up, which requires more fuel, which increases the mass, and so on.
|
||||
- To calculate the total fuel needed: start with the payload mass, calculate the fuel needed for that, add that fuel to the total mass, and calculate again. Repeat this process until the additional fuel required is less than 1 kg, then stop.
|
||||
- Ignore the mass of the rocket itself. Only compute fuel needed to lift the payload and its own fuel.
|
||||
|
||||
For example, given a payload mass of 50 kg, you would need 10 kg of fuel to lift it (payload / 5), which increases the total mass to 60 kg, which needs 12 kg to lift (2 additional kg), which increases the total mass to 62 kg, which needs 12.4 kg to lift - 0.4 additional kg - which is less 1 additional kg, so we stop here. The total mass to lift is 62.4 kg, 50 of which is the initial payload and 12.4 of fuel.
|
||||
|
||||
- Return the amount of fuel needed rounded to one decimal place.
|
||||
|
||||
# --hints--
|
||||
|
||||
`launch_fuel(50)` should return `12.4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(launch_fuel(50), 12.4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`launch_fuel(500)` should return `124.8`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(launch_fuel(500), 124.8)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`launch_fuel(243)` should return `60.7`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(launch_fuel(243), 60.7)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`launch_fuel(11000)` should return `2749.8`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(launch_fuel(11000), 2749.8)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`launch_fuel(6214)` should return `1553.4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(launch_fuel(6214), 1553.4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def launch_fuel(payload):
|
||||
|
||||
return payload
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def launch_fuel(payload):
|
||||
total_mass = payload
|
||||
additional_fuel = total_mass / 5
|
||||
total_fuel = additional_fuel
|
||||
|
||||
while additional_fuel >= 1:
|
||||
total_mass += additional_fuel
|
||||
additional_fuel = (total_mass / 5) - total_fuel
|
||||
total_fuel += additional_fuel
|
||||
|
||||
return round(total_fuel, 1)
|
||||
```
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 68c497f3aaefc9fd9f1b0e26
|
||||
title: "Challenge 62: Hex to Decimal"
|
||||
challengeType: 29
|
||||
dashedName: challenge-62
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a hexadecimal number (base 16), return its decimal (base 10) value as an integer.
|
||||
|
||||
Hexadecimal is a number system that uses 16 digits:
|
||||
|
||||
- `0-9` represent values `0` through `9`.
|
||||
- `A-F` represent values `10` through `15`.
|
||||
|
||||
Here's a partial conversion table:
|
||||
|
||||
| Hexadecimal | Decimal |
|
||||
|:-----------:|:-------:|
|
||||
| 0 | 0 |
|
||||
| 1 | 1 |
|
||||
| ... | ... |
|
||||
| 9 | 9 |
|
||||
| A | 10 |
|
||||
| ... | ... |
|
||||
| F | 15 |
|
||||
| 10 | 16 |
|
||||
| ... | ... |
|
||||
| 9F | 159 |
|
||||
| A0 | 160 |
|
||||
| ... | ... |
|
||||
| FF | 255 |
|
||||
| 100 | 256 |
|
||||
|
||||
- The string will only contain characters `0–9` and `A–F`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`hex_to_decimal("A")` should return `10`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(hex_to_decimal("A"), 10)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`hex_to_decimal("15")` should return `21`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(hex_to_decimal("15"), 21)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`hex_to_decimal("2E")` should return `46`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(hex_to_decimal("2E"), 46)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`hex_to_decimal("FF")` should return `255`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(hex_to_decimal("FF"), 255)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`hex_to_decimal("A3F")` should return `2623`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(hex_to_decimal("A3F"), 2623)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def hex_to_decimal(hex):
|
||||
|
||||
return hex
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def hex_to_decimal(hex):
|
||||
|
||||
return int(hex, 16)
|
||||
```
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da001
|
||||
title: "Challenge 63: Battle of Words"
|
||||
challengeType: 29
|
||||
dashedName: challenge-63
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two sentences representing your team and an opposing team, where each word from your team battles the corresponding word from the opposing team, determine which team wins using the following rules:
|
||||
|
||||
- The given sentences will always contain the same number of words.
|
||||
- Words are separated by a single space and will only contain letters.
|
||||
- The value of each word is the sum of its letters.
|
||||
- Letters `a` to `z` correspond to the values `1` through `26`. For example, `a` is `1`, and `z` is `26`.
|
||||
- A capital letter doubles the value of the letter. For example, `A` is `2`, and `Z` is `52`.
|
||||
- Words battle in order: the first word of your team battles the first word of the opposing team, and so on.
|
||||
- A word wins if its value is greater than the opposing word's value.
|
||||
- The team with more winning words is the winner.
|
||||
|
||||
Return `"We win"` if your team is the winner, `"We lose"` if your team loses, and `"Draw"` if both teams have the same number of wins.
|
||||
|
||||
# --hints--
|
||||
|
||||
`battle("hello world", "hello word")` should return `"We win"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("hello world", "hello word"), "We win")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("Hello world", "hello world")` should return `"We win"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("Hello world", "hello world"), "We win")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("lorem ipsum", "kitty ipsum")` should return `"We lose"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("lorem ipsum", "kitty ipsum"), "We lose")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("hello world", "world hello")` should return `"Draw"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("hello world", "world hello"), "Draw")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("git checkout", "git switch")` should return `"We win"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("git checkout", "git switch"), "We win")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("Cheeseburger with fries", "Cheeseburger with Fries")` should return `"We lose"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("Cheeseburger with fries", "Cheeseburger with Fries"), "We lose")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`battle("We must never surrender", "Our team must win")` should return `"Draw"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(battle("We must never surrender", "Our team must win"), "Draw")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def battle(our_team, opponent):
|
||||
|
||||
return our_team
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_word_value(word):
|
||||
value = 0
|
||||
for char in word:
|
||||
val = ord(char.lower()) - ord('a') + 1
|
||||
if char.isupper():
|
||||
val *= 2
|
||||
value += val
|
||||
return value
|
||||
|
||||
def battle(our_team, opponent):
|
||||
my_wins = 0
|
||||
opp_wins = 0
|
||||
|
||||
for my_word, opp_word in zip(our_team.split(), opponent.split()):
|
||||
my_val = get_word_value(my_word)
|
||||
opp_val = get_word_value(opp_word)
|
||||
|
||||
if my_val > opp_val:
|
||||
my_wins += 1
|
||||
elif opp_val > my_val:
|
||||
opp_wins += 1
|
||||
|
||||
if my_wins > opp_wins:
|
||||
return "We win"
|
||||
elif opp_wins > my_wins:
|
||||
return "We lose"
|
||||
else:
|
||||
return "Draw"
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da002
|
||||
title: "Challenge 64: 24 to 12"
|
||||
challengeType: 29
|
||||
dashedName: challenge-64
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a time of the day in the 24-hour format of `"HHMM"`, return the time in its equivalent 12-hour format of `"H:MM AM"` or `"H:MM PM"`.
|
||||
|
||||
- The given input will always be a four-digit string in 24-hour time format, from `"0000"` to `"2359"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`to_12("1124")` should return `"11:24 AM"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_12("1124"), "11:24 AM")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_12("0900")` should return `"9:00 AM"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_12("0900"), "9:00 AM")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_12("1455")` should return `"2:55 PM"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_12("1455"), "2:55 PM")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_12("2346")` should return `"11:46 PM"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_12("2346"), "11:46 PM")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`to_12("0030")` should return `"12:30 AM"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(to_12("0030"), "12:30 AM")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def to_12(time):
|
||||
|
||||
return time
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def convert_hours(hours):
|
||||
if hours == 0:
|
||||
return 12
|
||||
elif hours > 12:
|
||||
return hours - 12
|
||||
else:
|
||||
return hours
|
||||
|
||||
def to_12(time):
|
||||
hours = int(time[:2])
|
||||
minutes = time[2:]
|
||||
period = "AM" if hours < 12 else "PM"
|
||||
|
||||
return f"{convert_hours(hours)}:{minutes} {period}"
|
||||
```
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da003
|
||||
title: "Challenge 65: String Count"
|
||||
challengeType: 29
|
||||
dashedName: challenge-65
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two strings, determine how many times the second string appears in the first.
|
||||
|
||||
- The pattern string can overlap in the first string. For example, `"aaa"` contains `"aa"` twice. The first two `a`'s and the second two.
|
||||
|
||||
# --hints--
|
||||
|
||||
`count('abcdefg', 'def')` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count('abcdefg', 'def'), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count('hello', 'world')` should return `0`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count('hello', 'world'), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count('mississippi', 'iss')` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count('mississippi', 'iss'), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count('she sells seashells by the seashore', 'sh')` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count('she sells seashells by the seashore', 'sh'), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count('101010101010101010101', '101')` should return `10`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count('101010101010101010101', '101'), 10)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def count(text, parameter):
|
||||
|
||||
return text
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def count(text, pattern):
|
||||
if not pattern:
|
||||
return 0
|
||||
occurrences = 0
|
||||
|
||||
for i in range(len(text) - len(pattern) + 1):
|
||||
if text[i:i+len(pattern)] == pattern:
|
||||
occurrences += 1
|
||||
|
||||
return occurrences
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da004
|
||||
title: "Challenge 66: HTML Tag Stripper"
|
||||
challengeType: 29
|
||||
dashedName: challenge-66
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of HTML code, remove the tags and return the plain text content.
|
||||
|
||||
- The input string will contain only valid HTML.
|
||||
- HTML tags may be nested.
|
||||
- Remove the tags and any attributes.
|
||||
|
||||
For example, `'<a href="#">Click here</a>'` should return `"Click here"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`strip_tags('<a href="#">Click here</a>')` should return `"Click here"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(strip_tags('<a href="#">Click here</a>'), "Click here")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`strip_tags('<p class="center">Hello <b>World</b>!</p>')` should return `"Hello World!"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(strip_tags('<p class="center">Hello <b>World</b>!</p>'), "Hello World!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`strip_tags('<img src="cat.jpg" alt="Cat">')` should return an empty string (`""`).
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(strip_tags('<img src="cat.jpg" alt="Cat">'), "")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`strip_tags('<main id="main"><section class="section">section</section><section class="section">section</section></main>')` should return `sectionsection`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(strip_tags('<main id="main"><section class="section">section</section><section class="section">section</section></main>'), "sectionsection")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def strip_tags(html):
|
||||
|
||||
return html
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def strip_tags(html):
|
||||
return re.sub(r'<[^>]*>', '', html)
|
||||
```
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da005
|
||||
title: "Challenge 67: Email Validator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-67
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, determine if it is a valid email address using the following constraints:
|
||||
|
||||
- It must contain exactly one `@` symbol.
|
||||
- The local part (before the `@`):
|
||||
- Can only contain letters (`a-z`, `A-Z`), digits (`0-9`), dots (`.`), underscores (`_`), or hyphens (`-`).
|
||||
- Cannot start or end with a dot.
|
||||
- The domain part (after the `@`):
|
||||
- Must contain at least one dot.
|
||||
- Must end with a dot followed by at least two letters.
|
||||
- Neither the local or domain part can have two dots in a row.
|
||||
|
||||
# --hints--
|
||||
|
||||
`validate("a@b.cd")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("a@b.cd"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("hell.-w.rld@example.com")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("hell.-w.rld@example.com"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate(".b@sh.rc")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate(".b@sh.rc"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("example@test.c0")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("example@test.c0"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("freecodecamp.org")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("freecodecamp.org"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("develop.ment_user@c0D!NG.R.CKS")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("develop.ment_user@c0D!NG.R.CKS"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("hello.@wo.rld")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("hello.@wo.rld"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("hello@world..com")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("hello@world..com"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("develop..ment_user@c0D!NG.R.CKS")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("develop..ment_user@c0D!NG.R.CKS"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`validate("git@commit@push.io")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(validate("git@commit@push.io"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def validate(email):
|
||||
|
||||
return email
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def validate(email):
|
||||
if '..' in email:
|
||||
return False
|
||||
|
||||
parts = email.split('@')
|
||||
if len(parts) != 2:
|
||||
return False
|
||||
|
||||
local, domain = parts
|
||||
|
||||
if local.startswith('.') or local.endswith('.'):
|
||||
return False
|
||||
if not re.match(r'^[a-zA-Z0-9._-]+$', local):
|
||||
return False
|
||||
|
||||
if '.' not in domain:
|
||||
return False
|
||||
|
||||
tld = domain.split('.')[-1]
|
||||
if len(tld) < 2 or not tld.isalpha():
|
||||
return False
|
||||
|
||||
return True
|
||||
```
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da006
|
||||
title: "Challenge 68: Credit Card Masker"
|
||||
challengeType: 29
|
||||
dashedName: challenge-68
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of credit card numbers, return a masked version of it using the following constraints:
|
||||
|
||||
- The string will contain four sets of four digits (`0-9`), with all sets being separated by a single space, or a single hyphen (`-`).
|
||||
- Replace all numbers, except the last four, with an asterisk (`*`).
|
||||
- Leave the remaining characters unchanged.
|
||||
|
||||
For example, given `"4012-8888-8888-1881"` return `"****-****-****-1881"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`mask("4012-8888-8888-1881")` should return `"****-****-****-1881"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mask("4012-8888-8888-1881"), "****-****-****-1881")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mask("5105 1051 0510 5100")` should return `"**** **** **** 5100"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mask("5105 1051 0510 5100"), "**** **** **** 5100")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mask("6011 1111 1111 1117")` should return `"**** **** **** 1117"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mask("6011 1111 1111 1117"), "**** **** **** 1117")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`mask("2223-0000-4845-0010")` should return `"****-****-****-0010"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(mask("2223-0000-4845-0010"), "****-****-****-0010")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def mask(card):
|
||||
|
||||
return card
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def mask(card):
|
||||
if '-' in card:
|
||||
split = card.split('-')
|
||||
return f"****-****-****-{split[3]}"
|
||||
else:
|
||||
split = card.split(' ')
|
||||
return f"**** **** **** {split[3]}"
|
||||
```
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da007
|
||||
title: "Challenge 69: Missing Socks"
|
||||
challengeType: 29
|
||||
dashedName: challenge-69
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer representing the number of pairs of socks you started with, and another integer representing how many wash cycles you have gone through, return the number of complete pairs of socks you currently have using the following constraints:
|
||||
|
||||
- Every 2 wash cycles, you lose a single sock.
|
||||
- Every 3 wash cycles, you find a single missing sock.
|
||||
- Every 5 wash cycles, a single sock is worn out and must be thrown away.
|
||||
- Every 10 wash cycles, you buy a pair of socks.
|
||||
- You can never have less than zero total socks.
|
||||
- Rules can overlap. For example, on wash cycle 10, you will lose a single sock, throw away a single sock, and buy a new pair of socks.
|
||||
- Return the number of complete pairs of socks.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sock_pairs(2, 5)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sock_pairs(2, 5), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sock_pairs(1, 2)` should return `0`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sock_pairs(1, 2), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sock_pairs(5, 11)` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sock_pairs(5, 11), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sock_pairs(6, 25)` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sock_pairs(6, 25), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sock_pairs(1, 8)` should return `0`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sock_pairs(1, 8), 0)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sock_pairs(pairs, cycles):
|
||||
|
||||
return pairs
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sock_pairs(pairs, cycles):
|
||||
socks = pairs * 2
|
||||
|
||||
for i in range(1, cycles + 1):
|
||||
if i % 2 == 0:
|
||||
socks -= 1
|
||||
if i % 3 == 0:
|
||||
socks += 1
|
||||
if i % 5 == 0:
|
||||
socks -= 1
|
||||
if i % 10 == 0:
|
||||
socks += 2
|
||||
|
||||
if socks < 0:
|
||||
socks = 0
|
||||
|
||||
return socks // 2
|
||||
```
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da008
|
||||
title: "Challenge 70: HTML Attribute Extractor"
|
||||
challengeType: 29
|
||||
dashedName: challenge-70
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of a valid HTML element, return the attributes of the element using the following criteria:
|
||||
|
||||
- You will only be given one element.
|
||||
- Attributes will be in the format: `attribute="value"`.
|
||||
- Return an array of strings with each attribute property and value, separated by a comma, in this format: `["attribute1, value1", "attribute2, value2"]`.
|
||||
- Return attributes in the order they are given.
|
||||
- If no attributes are found, return an empty array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`extract_attributes('<span class="red"></span>')` should return `["class, red"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(extract_attributes('<span class="red"></span>'), ["class, red"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`extract_attributes('<meta charset="UTF-8" />')` should return `["charset, UTF-8"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(extract_attributes('<meta charset="UTF-8" />'), ["charset, UTF-8"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`extract_attributes("<p>Lorem ipsum dolor sit amet</p>")` should return `[]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(extract_attributes("<p>Lorem ipsum dolor sit amet</p>"), [])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`extract_attributes('<input name="email" type="email" required="true" />')` should return `["name, email", "type, email", "required, true"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(extract_attributes('<input name="email" type="email" required="true" />'), ["name, email", "type, email", "required, true"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`extract_attributes('<button id="submit" class="btn btn-primary">Submit</button>')` should return `["id, submit", "class, btn btn-primary"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(extract_attributes('<button id="submit" class="btn btn-primary">Submit</button>'), ["id, submit", "class, btn btn-primary"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def extract_attributes(element):
|
||||
|
||||
return element
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
def extract_attributes(element):
|
||||
pattern = r'([\w-]+)="([^"]*)"'
|
||||
matches = re.findall(pattern, element)
|
||||
return [f"{attr}, {val}" for attr, val in matches]
|
||||
```
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da009
|
||||
title: "Challenge 71: Tip Calculator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-71
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the price of your meal and a custom tip percent, return an array with three tip values; 15%, 20%, and the custom amount.
|
||||
|
||||
- Prices will be given in the format: `"$N.NN"`.
|
||||
- Custom tip percents will be given in this format: `"25%"`.
|
||||
- Return amounts in the same `"$N.NN"` format, rounded to two decimal places.
|
||||
|
||||
For example, given a `"$10.00"` meal price, and a `"25%"` custom tip value, return `["$1.50", "$2.00", "$2.50"]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`calculate_tips("$10.00", "25%")` should return `["$1.50", "$2.00", "$2.50"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(calculate_tips("$10.00", "25%"), ["$1.50", "$2.00", "$2.50"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`calculate_tips("$89.67", "26%")` should return `["$13.45", "$17.93", "$23.31"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(calculate_tips("$89.67", "26%"), ["$13.45", "$17.93", "$23.31"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`calculate_tips("$19.85", "9%")` should return `["$2.98", "$3.97", "$1.79"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(calculate_tips("$19.85", "9%"), ["$2.98", "$3.97", "$1.79"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def calculate_tips(meal_price, custom_tip):
|
||||
|
||||
return meal_price
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def calculate_tips(meal_price, custom_tip):
|
||||
meal = float(meal_price[1:])
|
||||
custom_percent = float(custom_tip[:-1])
|
||||
|
||||
tips_percent = [15, 20, custom_percent]
|
||||
tips = [f"${meal * p / 100:.2f}" for p in tips_percent]
|
||||
|
||||
return tips
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 68cae5b538ff798bbd4da00a
|
||||
title: "Challenge 72: Thermostat Adjuster 2"
|
||||
challengeType: 29
|
||||
dashedName: challenge-72
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the current temperature of a room in Fahrenheit and a target temperature in Celsius, return a string indicating how to adjust the room temperature based on these constraints:
|
||||
|
||||
- Return `"Heat: X degrees Fahrenheit"` if the current temperature is below the target. With `X` being the number of degrees in Fahrenheit to heat the room to reach the target, rounded to 1 decimal place.
|
||||
- Return `"Cool: X degrees Fahrenheit"` if the current temperature is above the target. With `X` being the number of degrees in Fahrenheit to cool the room to reach the target, rounded to 1 decimal place.
|
||||
- Return `"Hold"` if the current temperature is equal to the target.
|
||||
|
||||
To convert Celsius to Fahrenheit, multiply the Celsius temperature by 1.8 and add 32 to the result (`F = (C * 1.8) + 32`).
|
||||
|
||||
# --hints--
|
||||
|
||||
`adjust_thermostat(32, 0)` should return `"Hold"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(32, 0), "Hold")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(70, 25)` should return `"Heat: 7.0 degrees Fahrenheit"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(70, 25), "Heat: 7.0 degrees Fahrenheit")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(72, 18)` should return `"Cool: 7.6 degrees Fahrenheit"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(72, 18), "Cool: 7.6 degrees Fahrenheit")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(212, 100)` should return `"Hold"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(212, 100), "Hold")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`adjust_thermostat(59, 22)` should return `"Heat: 12.6 degrees Fahrenheit"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(adjust_thermostat(59, 22), "Heat: 12.6 degrees Fahrenheit")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def adjust_thermostat(current_f, target_c):
|
||||
|
||||
return current_f
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def adjust_thermostat(current_f, target_c):
|
||||
target_f = (target_c * 1.8) + 32
|
||||
diff = round(abs(target_f - current_f), 1)
|
||||
|
||||
if current_f < target_f:
|
||||
return f"Heat: {diff} degrees Fahrenheit"
|
||||
elif current_f > target_f:
|
||||
return f"Cool: {diff} degrees Fahrenheit"
|
||||
else:
|
||||
return "Hold"
|
||||
```
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 68d2ba1468508398389487ce
|
||||
title: "Challenge 73: Speak Wisely, You Must"
|
||||
challengeType: 29
|
||||
dashedName: challenge-73
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a sentence, return a version of it that sounds like advice from a wise teacher using the following rules:
|
||||
|
||||
- Words are separated by a single space.
|
||||
- Find the first occurrence of one of the following words in the sentence: `"have"`, `"must"`, `"are"`, `"will"`, `"can"`.
|
||||
- Move all words before and including that word to the end of the sentence and:
|
||||
- Preserve the order of the words when you move them.
|
||||
- Make them all lowercase.
|
||||
- And add a comma and space before them.
|
||||
- Capitalize the first letter of the new first word of the sentence.
|
||||
- All given sentences will end with a single punctuation mark. Keep the original punctuation of the sentence and move it to the end of the new sentence.
|
||||
- Return the new sentence, make sure there's a single space between each word and no spaces at the beginning or end of the sentence.
|
||||
|
||||
For example, given `"You must speak wisely."` return `"Speak wisely, you must."`
|
||||
|
||||
# --hints--
|
||||
|
||||
`wise_speak("You must speak wisely.")` should return `"Speak wisely, you must."`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(wise_speak("You must speak wisely."), "Speak wisely, you must.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`wise_speak("You can do it!")` should return `"Do it, you can!"`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(wise_speak("You can do it!"), "Do it, you can!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`wise_speak("Do you think you will complete this?")` should return `"Complete this, do you think you will?"`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(wise_speak("Do you think you will complete this?"), "Complete this, do you think you will?")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`wise_speak("All your base are belong to us.")` should return `"Belong to us, all your base are."`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(wise_speak("All your base are belong to us."), "Belong to us, all your base are.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`wise_speak("You have much to learn.")` should return `"Much to learn, you have."`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(wise_speak("You have much to learn."), "Much to learn, you have.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def wise_speak(sentence):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def wise_speak(sentence):
|
||||
triggers = ["have", "must", "are", "will", "can"]
|
||||
punctuation = sentence[-1]
|
||||
words = sentence[:-1].split(" ")
|
||||
|
||||
index = next(i for i, w in enumerate(words) if w in triggers)
|
||||
|
||||
to_move = [w.lower() for w in words[:index + 1]]
|
||||
remaining = words[index + 1:]
|
||||
|
||||
if remaining:
|
||||
remaining[0] = remaining[0][0].upper() + remaining[0][1:]
|
||||
|
||||
return " ".join(remaining) + ", " + " ".join(to_move) + punctuation
|
||||
```
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 68d2ba1468508398389487cf
|
||||
title: "Challenge 74: Favorite Songs"
|
||||
challengeType: 29
|
||||
dashedName: challenge-74
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Remember iPods? The first model came out 24 years ago today, on Oct. 23, 2001.
|
||||
|
||||
Given an array of song objects representing your iPod playlist, return an array with the titles of the two most played songs, with the most played song first.
|
||||
|
||||
- Each object will have a `"title"` property (string), and a `"plays"` property (integer).
|
||||
|
||||
# --hints--
|
||||
|
||||
`favorite_songs([{"title": "Sync or Swim", "plays": 3}, {"title": "Byte Me", "plays": 1}, {"title": "Earbud Blues", "plays": 2} ])` should return `["Sync or Swim", "Earbud Blues"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(favorite_songs([{"title": "Sync or Swim", "plays": 3}, {"title": "Byte Me", "plays": 1}, {"title": "Earbud Blues", "plays": 2} ]), ["Sync or Swim", "Earbud Blues"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`favorite_songs([{"title": "Skip Track", "plays": 98}, {"title": "99 Downloads", "plays": 99}, {"title": "Clickwheel Love", "plays": 100} ])` should return `["Clickwheel Love", "99 Downloads"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(favorite_songs([{"title": "Skip Track", "plays": 98}, {"title": "99 Downloads", "plays": 99}, {"title": "Clickwheel Love", "plays": 100} ]), ["Clickwheel Love", "99 Downloads"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`favorite_songs([{"title": "Song A", "plays": 42}, {"title": "Song B", "plays": 99}, {"title": "Song C", "plays": 75} ])` should return `["Song B", "Song C"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(favorite_songs([{"title": "Song A", "plays": 42}, {"title": "Song B", "plays": 99}, {"title": "Song C", "plays": 75} ]), ["Song B", "Song C"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def favorite_songs(playlist):
|
||||
|
||||
return playlist
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def favorite_songs(playlist):
|
||||
sorted_songs = sorted(playlist, key=lambda x: x["plays"], reverse=True)
|
||||
return [song["title"] for song in sorted_songs[:2]]
|
||||
```
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
---
|
||||
id: 68d2ba1468508398389487d0
|
||||
title: "Challenge 75: Hidden Treasure"
|
||||
challengeType: 29
|
||||
dashedName: challenge-75
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a 2D array representing a map of the ocean floor that includes a hidden treasure, and an array with the coordinates (`[row, column]`) for the next dive of your treasure search, return `"Empty"`, `"Found"`, or `"Recovered"` using the following rules:
|
||||
|
||||
- The given 2D array will contain exactly one unrecovered treasure, which will occupy multiple cells.
|
||||
- Each cell in the 2D array will contain one of the following values:
|
||||
- `"-"`: No treasure.
|
||||
- `"O"`: A part of the treasure that has not been found.
|
||||
- `"X"`: A part of the treasure that has already been found.
|
||||
|
||||
- If the dive location has no treasure, return `"Empty"`.
|
||||
- If the dive location finds treasure, but at least one other part of the treasure remains unfound, return `"Found"`.
|
||||
- If the dive location finds the last unfound part of the treasure, return `"Recovered"`.
|
||||
|
||||
For example, given:
|
||||
|
||||
```json
|
||||
[
|
||||
[ "-", "X"],
|
||||
[ "-", "X"],
|
||||
[ "-", "O"]
|
||||
]
|
||||
```
|
||||
|
||||
And `[2, 1]` for the coordinates of the dive location, return `"Recovered"` because the dive found the last unfound part of the treasure.
|
||||
|
||||
# --hints--
|
||||
|
||||
`dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 1])` should return `"Recovered"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 1]), "Recovered")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 0])` should return `"Empty"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 0]), "Empty")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`dive([[ "-", "X"], [ "-", "O"], [ "-", "O"]], [1, 1])` should return `"Found"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "X"], [ "-", "O"], [ "-", "O"]], [1, 1]), "Found")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`dive([[ "-", "-", "-"], [ "X", "O", "X"], [ "-", "-", "-"]], [1, 2])` should return `"Found"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "-", "-"], [ "X", "O", "X"], [ "-", "-", "-"]], [1, 2]), "Found")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [2, 0])` should return `"Recovered"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [2, 0]), "Recovered")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [1, 2])` should return `"Empty"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [1, 2]), "Empty")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def dive(map, coordinates):
|
||||
|
||||
return map
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def dive(map, coordinates):
|
||||
row, col = coordinates
|
||||
target = map[row][col]
|
||||
|
||||
if target == "-":
|
||||
return "Empty"
|
||||
|
||||
if target == "O":
|
||||
for r in range(len(map)):
|
||||
for c in range(len(map[r])):
|
||||
if (r != row or c != col) and map[r][c] == "O":
|
||||
return "Found"
|
||||
return "Recovered"
|
||||
|
||||
return "Found"
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 68d30845cc08266018fc46bc
|
||||
title: "Challenge 76: Complementary DNA"
|
||||
challengeType: 29
|
||||
dashedName: challenge-76
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a DNA sequence, return its complementary strand using the following rules:
|
||||
|
||||
- DNA consists of the letters `"A"`, `"C"`, `"G"`, and `"T"`.
|
||||
- The letters `"A"` and `"T"` complement each other.
|
||||
- The letters `"C"` and `"G"` complement each other.
|
||||
|
||||
For example, given `"ACGT"`, return `"TGCA"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`complementary_dna("ACGT")` should return `"TGCA"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(complementary_dna("ACGT"), "TGCA")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`complementary_dna("ATGCGTACGTTAGC")` should return `"TACGCATGCAATCG"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(complementary_dna("ATGCGTACGTTAGC"), "TACGCATGCAATCG")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`complementary_dna("GGCTTACGATCGAAG")` should return `"CCGAATGCTAGCTTC"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(complementary_dna("GGCTTACGATCGAAG"), "CCGAATGCTAGCTTC")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`complementary_dna("GATCTAGCTAGGCTAGCTAG")` should return `"CTAGATCGATCCGATCGATC"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(complementary_dna("GATCTAGCTAGGCTAGCTAG"), "CTAGATCGATCCGATCGATC")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def complementary_dna(strand):
|
||||
|
||||
return strand
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def complementary_dna(strand):
|
||||
complements = { "A": "T", "T": "A", "C": "G", "G": "C" }
|
||||
return "".join(complements[n] for n in strand)
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 68d30845cc08266018fc46bd
|
||||
title: "Challenge 77: Duration Formatter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-77
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer number of seconds, return a string representing the same duration in the format `"H:MM:SS"`, where `"H"` is the number of hours, `"MM"` is the number of minutes, and `"SS"` is the number of seconds. Return the time using the following rules:
|
||||
|
||||
- Seconds: Should always be two digits.
|
||||
- Minutes: Should omit leading zeros when they aren't needed. Use `"0"` if the duration is less than one minute.
|
||||
- Hours: Should be included only if they're greater than zero.
|
||||
|
||||
# --hints--
|
||||
|
||||
`format(500)` should return `"8:20"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format(500), "8:20")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`format(4000)` should return `"1:06:40"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format(4000), "1:06:40")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`format(1)` should return `"0:01"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format(1), "0:01")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`format(5555)` should return `"1:32:35"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format(5555), "1:32:35")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`format(99999)` should return `"27:46:39"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(format(99999), "27:46:39")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def format(seconds):
|
||||
|
||||
return seconds
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def format(seconds):
|
||||
h = seconds // 3600
|
||||
m = (seconds % 3600) // 60
|
||||
s = seconds % 60
|
||||
|
||||
seconds_str = f"{s:02d}"
|
||||
minutes_str = str(m)
|
||||
|
||||
if h > 0:
|
||||
hours_str = str(h)
|
||||
return f"{hours_str}:{minutes_str.zfill(2)}:{seconds_str}"
|
||||
else:
|
||||
return f"{minutes_str}:{seconds_str}"
|
||||
```
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 68d30845cc08266018fc46be
|
||||
title: "Challenge 78: Integer Sequence"
|
||||
challengeType: 29
|
||||
dashedName: challenge-78
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a positive integer, return a string with all of the integers from `1` up to, and including, the given number, in numerical order.
|
||||
|
||||
For example, given `5`, return `"12345"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sequence(5)` should return `"12345"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sequence(5), "12345")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sequence(10)` should return `"12345678910"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sequence(10), "12345678910")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sequence(1)` should return `"1"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sequence(1), "1")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sequence(27)` should return `"123456789101112131415161718192021222324252627"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sequence(27), "123456789101112131415161718192021222324252627")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sequence(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sequence(n):
|
||||
result = ""
|
||||
for i in range(1, n + 1):
|
||||
result += str(i)
|
||||
return result
|
||||
```
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
---
|
||||
id: 68d30fc57588d97fd3027b30
|
||||
title: "Challenge 79: Navigator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-79
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On October 28, 1994, Netscape Navigator was released, helping millions explore the early web.
|
||||
|
||||
Given an array of browser commands you executed on Netscape Navigator, return the current page you are on after executing all the commands using the following rules:
|
||||
|
||||
- You always start on the `"Home"` page, which will not be included in the commands array.
|
||||
- Valid commands are:
|
||||
- `"Visit Page"`: Where `"Page"` is the name of the page you are visiting. For example, `"Visit About"` takes you to the `"About"` page. When you visit a new page, make sure to discard any forward history you have.
|
||||
- `"Back"`: Takes you to the previous page in your history or stays on the current page if there isn't one.
|
||||
- `"Forward"`: Takes you forward in the history to the page you came from or stays on the current page if there isn't one.
|
||||
|
||||
For example, given `["Visit About Us", "Back", "Forward"]`, return `"About Us"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`navigate(["Visit About Us", "Back", "Forward"])` should return `"About Us"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Visit About Us", "Back", "Forward"]), "About Us")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Forward"])` should return `"Home"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Forward"]), "Home")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Back"])` should return `"Home"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Back"]), "Home")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Visit About Us", "Visit Gallery"])` should return `"Gallery"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Visit About Us", "Visit Gallery"]), "Gallery")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Visit About Us", "Visit Gallery", "Back", "Back"])` should return `"Home"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Visit About Us", "Visit Gallery", "Back", "Back"]), "Home")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Visit About", "Visit Gallery", "Back", "Visit Contact", "Forward"])` should return `"Contact"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Visit About", "Visit Gallery", "Back", "Visit Contact", "Forward"]), "Contact")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`navigate(["Visit About Us", "Visit Visit Us", "Forward", "Visit Contact Us", "Back"])` should return `"Visit Us"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(navigate(["Visit About Us", "Visit Visit Us", "Forward", "Visit Contact Us", "Back"]), "Visit Us")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def navigate(commands):
|
||||
|
||||
return commands
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def navigate(commands):
|
||||
history = ["Home"]
|
||||
current_index = 0
|
||||
|
||||
for command in commands:
|
||||
if command.startswith("Visit "):
|
||||
history = history[:current_index + 1]
|
||||
history.append(command[6:])
|
||||
current_index += 1
|
||||
elif command == "Back" and current_index > 0:
|
||||
current_index -= 1
|
||||
elif command == "Forward" and current_index < len(history) - 1:
|
||||
current_index += 1
|
||||
|
||||
return history[current_index]
|
||||
```
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd62
|
||||
title: "Challenge 80: Email Sorter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-80
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On October 29, 1971, the first email ever was sent, introducing the `username@domain` format we still use. Now, there are billions of email addresses.
|
||||
|
||||
In this challenge, you are given a list of email addresses and need to sort them alphabetically by domain name first (the part after the `@`), and username second (the part before the `@`).
|
||||
|
||||
- Sorting should be case-insensitive.
|
||||
- If more than one email has the same domain, sort them by their username.
|
||||
- Return an array of the sorted addresses.
|
||||
- Returned addresses should retain their original case.
|
||||
|
||||
For example, given `["jill@mail.com", "john@example.com", "jane@example.com"]`, return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sort(["jill@mail.com", "john@example.com", "jane@example.com"])` should return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["jill@mail.com", "john@example.com", "jane@example.com"]), ["jane@example.com", "john@example.com", "jill@mail.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"])` should return `["bob@mail.com", "carol@mail.com", "alice@zoo.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"]), ["bob@mail.com", "carol@mail.com", "alice@zoo.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["user@z.com", "user@y.com", "user@x.com"])` should return `["user@x.com", "user@y.com", "user@z.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["user@z.com", "user@y.com", "user@x.com"]), ["user@x.com", "user@y.com", "user@z.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"])` should return `["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"]), ["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"])` should return `["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"]), ["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sort(emails):
|
||||
|
||||
return emails
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sort(emails):
|
||||
return sorted(
|
||||
emails,
|
||||
key=lambda email: (email.split('@')[1].lower(), email.split('@')[0].lower())
|
||||
)
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd63
|
||||
title: "Challenge 81: Nth Prime"
|
||||
challengeType: 29
|
||||
dashedName: challenge-81
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A prime number is a positive integer greater than 1 that is divisible only by 1 and itself. The first five prime numbers are `2`, `3`, `5`, `7`, and `11`.
|
||||
|
||||
Given a positive integer `n`, return the `n`th prime number. For example, given `5` return the 5th prime number: `11`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`nth_prime(5)` should return `11`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(5), 11)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(10)` should return `29`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(10), 29)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(16)` should return `53`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(16), 53)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(99)` should return `523`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(99), 523)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(1000)` should return `7919`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(1000), 7919)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def nth_prime(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def nth_prime(n):
|
||||
primes = []
|
||||
num = 2
|
||||
|
||||
while len(primes) < n:
|
||||
is_prime = True
|
||||
for i in range(2, int(num**0.5) + 1):
|
||||
if num % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.append(num)
|
||||
num += 1
|
||||
|
||||
return primes[n - 1]
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd64
|
||||
title: "Challenge 82: SpOoKy~CaSe"
|
||||
challengeType: 29
|
||||
dashedName: challenge-82
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a variable name, convert it to "spooky case" using the following constraints:
|
||||
|
||||
- Replace all underscores (`_`), and hyphens (`-`) with a tilde (`~`).
|
||||
- Capitalize the first letter of the string, and every other letter after that. Ignore the tilde character when counting. Make all other letters lowercase.
|
||||
|
||||
For example, given `hello_world`, return `HeLlO~wOrLd`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`spookify("hello_world")` should return `"HeLlO~wOrLd"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("hello_world"), "HeLlO~wOrLd")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("Spooky_Case")` should return `"SpOoKy~CaSe"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("Spooky_Case"), "SpOoKy~CaSe")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("TRICK-or-TREAT")` should return `"TrIcK~oR~tReAt"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("TRICK-or-TREAT"), "TrIcK~oR~tReAt")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("c_a-n_d-y_-b-o_w_l")` should return `"C~a~N~d~Y~~b~O~w~L"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("c_a-n_d-y_-b-o_w_l"), "C~a~N~d~Y~~b~O~w~L")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts")` should return `"ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts"), "ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def spookify(boo):
|
||||
|
||||
return boo
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def spookify(boo):
|
||||
replaced = boo.replace("_", "~").replace("-", "~")
|
||||
|
||||
result = []
|
||||
capitalize = True
|
||||
|
||||
for char in replaced:
|
||||
if char == "~":
|
||||
result.append(char)
|
||||
else:
|
||||
result.append(char.upper() if capitalize else char.lower())
|
||||
capitalize = not capitalize
|
||||
|
||||
return "".join(result)
|
||||
```
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd65
|
||||
title: "Challenge 83: Signature Validation"
|
||||
challengeType: 29
|
||||
dashedName: challenge-83
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a message string, a secret key string, and a signature number, determine if the signature is valid using this encoding method:
|
||||
|
||||
- Letters in the message and secret key have these values:
|
||||
- `a` to `z` have values `1` to `26` respectively.
|
||||
- `A` to `Z` have values `27` to `52` respectively.
|
||||
- All other characters have no value.
|
||||
- Compute the signature by taking the sum of the message plus the sum of the secret key.
|
||||
|
||||
For example, given the message `"foo"` and the secret key `"bar"`, the signature would be `57`:
|
||||
|
||||
```md
|
||||
f (6) + o (15) + o (15) = 36
|
||||
b (2) + a (1) + r (18) = 21
|
||||
36 + 21 = 57
|
||||
```
|
||||
|
||||
Check if the computed signature matches the provided signature.
|
||||
|
||||
# --hints--
|
||||
|
||||
`verify("foo", "bar", 57)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("foo", "bar", 57), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("foo", "bar", 54)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("foo", "bar", 54), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("freeCodeCamp", "Rocks", 238)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("freeCodeCamp", "Rocks", 238), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "No", 210)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Is this valid?", "No", 210), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "Yes", 233)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Is this valid?", "Yes", 233), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def verify(message, key, signature):
|
||||
|
||||
return message
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def verify(message, key, signature):
|
||||
def charValue(ch) -> int:
|
||||
if 'a' <= ch <= 'z':
|
||||
return ord(ch) - ord('a') + 1
|
||||
elif 'A' <= ch <= 'Z':
|
||||
return ord(ch) - ord('A') + 27
|
||||
else:
|
||||
return 0
|
||||
|
||||
def compute_sum(s):
|
||||
return sum(charValue(ch) for ch in s)
|
||||
|
||||
total = compute_sum(message) + compute_sum(key)
|
||||
return total == signature
|
||||
```
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd66
|
||||
title: "Challenge 84: Infected"
|
||||
challengeType: 29
|
||||
dashedName: challenge-84
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 2nd, 1988, the first major internet worm was released, infecting about 10% of computers connected to the internet after only a day.
|
||||
|
||||
In this challenge, you are given a number of days that have passed since an internet worm was released, and you need to determine how many computers are infected using the following rules:
|
||||
|
||||
- On day 0, the first computer is infected.
|
||||
- Each subsequent day, the number of infected computers doubles.
|
||||
- Every 3rd day, a patch is applied after the virus spreads and reduces the number of infected computers by 20%. Round the number of patched computers up to the nearest whole number.
|
||||
|
||||
For example, on:
|
||||
|
||||
- Day 0: 1 total computer is infected.
|
||||
- Day 1: 2 total computers are infected.
|
||||
- Day 2: 4 total computers are infected.
|
||||
- Day 3: 8 total computers are infected. Then, apply the patch: 8 infected * 20% = 1.6 patched. Round 1.6 up to 2. 8 computers infected - 2 patched = 6 total computers infected after day 3.
|
||||
|
||||
Return the number of total infected computers after the given amount of days have passed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`infected(1)` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(1), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(3)` should return `6`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(3), 6)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(8)` should return `152`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(8), 152)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(17)` should return `39808`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(17), 39808)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(25)` should return `5217638`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(25), 5217638)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def infected(days):
|
||||
|
||||
return days
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import math
|
||||
def infected(days):
|
||||
infected = 1
|
||||
for day in range(1, days + 1):
|
||||
infected *= 2
|
||||
if day % 3 == 0:
|
||||
patched = math.ceil(infected * 0.2)
|
||||
infected -= patched
|
||||
|
||||
return infected
|
||||
```
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a4
|
||||
title: "Challenge 85: Word Counter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-85
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a sentence string, return the number of words that are in the sentence.
|
||||
|
||||
- Words are any sequence of non-space characters and are separated by a single space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`count_words("Hello world")` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("Hello world"), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("The quick brown fox jumps over the lazy dog.")` should return `9`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("The quick brown fox jumps over the lazy dog."), 9)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("I like coding challenges!")` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("I like coding challenges!"), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("Complete the challenge in JavaScript and Python.")` should return `7`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("Complete the challenge in JavaScript and Python."), 7)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("The missing semi-colon crashed the entire internet.")` should return `7`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("The missing semi-colon crashed the entire internet."), 7)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def count_words(sentence):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def count_words(sentence):
|
||||
|
||||
return len(sentence.split(' '))
|
||||
```
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a5
|
||||
title: "Challenge 86: Image Search"
|
||||
challengeType: 29
|
||||
dashedName: challenge-86
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 4th, 2001, Google launched its image search, allowing people to find images using search terms. In this challenge, you will imitate the image search.
|
||||
|
||||
Given an array of image names and a search term, return an array of image names containing the search term.
|
||||
|
||||
- Ignore the case when matching the search terms.
|
||||
- Return the images in the same order they appear in the input array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`image_search(["dog.png", "cat.jpg", "parrot.jpeg"], "dog")` should return `["dog.png"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["dog.png", "cat.jpg", "parrot.jpeg"], "dog"), ["dog.png"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun")` should return `["Sunset.jpg", "sunflower.jpeg"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun"), ["Sunset.jpg", "sunflower.jpeg"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["Moon.png", "sun.jpeg", "stars.png"], "PNG")` should return `["Moon.png", "stars.png"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["Moon.png", "sun.jpeg", "stars.png"], "PNG"), ["Moon.png", "stars.png"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat")` should return `["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat"), ["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def image_search(images, term):
|
||||
|
||||
return images
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def image_search(images, term):
|
||||
lower_term = term.lower()
|
||||
return [img for img in images if lower_term in img.lower()]
|
||||
```
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a6
|
||||
title: "Challenge 87: Matrix Builder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-87
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two integers (a number of rows and a number of columns), return a matrix (an array of arrays) filled with zeros (`0`) of the given size.
|
||||
|
||||
For example, given `2` and `3`, return:
|
||||
|
||||
```json
|
||||
[
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
]
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`build_matrix(2, 3)` should return `[[0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(2, 3), [[0, 0, 0], [0, 0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(3, 2)` should return `[[0, 0], [0, 0], [0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(3, 2), [[0, 0], [0, 0], [0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(4, 3)` should return `[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(4, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(9, 1)` should return `[[0], [0], [0], [0], [0], [0], [0], [0], [0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(9, 1), [[0], [0], [0], [0], [0], [0], [0], [0], [0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def build_matrix(rows, cols):
|
||||
|
||||
return rows
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def build_matrix(rows, cols):
|
||||
|
||||
return [[0 for _ in range(cols)] for _ in range(rows)]
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a7
|
||||
title: "Challenge 88: Weekday Finder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-88
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string date in the format `YYYY-MM-DD`, return the day of the week.
|
||||
|
||||
Valid return days are:
|
||||
|
||||
- `"Sunday"`
|
||||
- `"Monday"`
|
||||
- `"Tuesday"`
|
||||
- `"Wednesday"`
|
||||
- `"Thursday"`
|
||||
- `"Friday"`
|
||||
- `"Saturday"`
|
||||
|
||||
Be sure to ignore time zones.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_weekday("2025-11-06")` should return `Thursday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2025-11-06"), "Thursday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("1999-12-31")` should return `Friday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("1999-12-31"), "Friday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("1111-11-11")` should return `Saturday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("1111-11-11"), "Saturday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("2112-12-21")` should return `Wednesday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2112-12-21"), "Wednesday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("2345-10-01")` should return `Monday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2345-10-01"), "Monday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_weekday(date_string):
|
||||
|
||||
return date_string
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import datetime
|
||||
def get_weekday(date_string):
|
||||
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
||||
year, month, day = map(int, date_string.split("-"))
|
||||
date = datetime.date(year, month, day)
|
||||
|
||||
return days[date.weekday() % 7 + 1 if date.weekday() != 6 else 0]
|
||||
```
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a8
|
||||
title: "Challenge 89: Counting Cards"
|
||||
challengeType: 29
|
||||
dashedName: challenge-89
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A standard deck of playing cards has 13 unique cards in each suit. Given an integer representing the number of cards to pick from the deck, return the number of unique combinations of cards you can pick.
|
||||
|
||||
- Order does not matter. Picking card A then card B is the same as picking card B then card A.
|
||||
|
||||
For example, given `52`, return `1`. There's only one combination of 52 cards to pick from a 52 card deck. And given `2`, return `1326`, There's 1326 card combinations you can end up with when picking 2 cards from the deck.
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinations(52)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(52), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(1)` should return `52`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(1), 52)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(2)` should return `1326`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(2), 1326)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(5)` should return `2598960`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(5), 2598960)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(10)` should return `15820024220`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(10), 15820024220)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(50)` should return `1326`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(50), 1326)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def combinations(cards):
|
||||
|
||||
return cards
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
from math import factorial
|
||||
def combinations(cards):
|
||||
n = 52
|
||||
|
||||
return factorial(n) // (factorial(cards) * factorial(n - cards))
|
||||
```
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c7c
|
||||
title: "Challenge 90: Character Limit"
|
||||
challengeType: 29
|
||||
dashedName: challenge-90
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this challenge, you are given a string and need to determine if it fits in a social media post. Return the following strings based on the rules given:
|
||||
|
||||
- `"short post"` if it fits within a 40-character limit.
|
||||
- `"long post"` if it's greater than 40 characters and fits within an 80-character limit.
|
||||
- `"invalid post"` if it's too long to fit within either limit.
|
||||
|
||||
# --hints--
|
||||
|
||||
`can_post("Hello world")` should return `"short post"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(can_post("Hello world"), "short post")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`can_post("This is a longer message but still under eighty characters.")` should return `"long post"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(can_post("This is a longer message but still under eighty characters."), "long post")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`can_post("This message is too long to fit into either of the character limits for a social media post.")` should return `"invalid post"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(can_post("This message is too long to fit into either of the character limits for a social media post."), "invalid post")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def can_post(message):
|
||||
|
||||
return message
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def can_post(message):
|
||||
if len(message) <= 40:
|
||||
return "short post"
|
||||
elif len(message) <= 80:
|
||||
return "long post"
|
||||
else:
|
||||
return "invalid post"
|
||||
```
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c7d
|
||||
title: "Challenge 91: Word Search"
|
||||
challengeType: 29
|
||||
dashedName: challenge-91
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a matrix (an array of arrays) of single letters and a word to find, return the start and end indices of the word in the matrix.
|
||||
|
||||
- The given matrix will be filled with all lowercase letters (`a-z`).
|
||||
- The word to find will always be in the matrix exactly once.
|
||||
- The word to find will always be in a straight line in one of these directions:
|
||||
- left to right
|
||||
- right to left
|
||||
- top to bottom
|
||||
- bottom to top
|
||||
|
||||
For example, given the matrix:
|
||||
|
||||
```md
|
||||
[
|
||||
["a", "c", "t"],
|
||||
["t", "a", "t"],
|
||||
["c", "t", "c"]
|
||||
]
|
||||
```
|
||||
|
||||
And the word `"cat"`, return:
|
||||
|
||||
```md
|
||||
[[0, 1], [2, 1]]
|
||||
```
|
||||
|
||||
Where `[0, 1]` are the indices for the `"c"` (start of the word), and `[2, 1]` are the indices for the `"t"` (end of the word).
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_word([["a", "c", "t"], ["t", "a", "t"], ["c", "t", "c"]], "cat")` should return `[[0, 1], [2, 1]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_word([["a", "c", "t"], ["t", "a", "t"], ["c", "t", "c"]], "cat"), [[0, 1], [2, 1]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_word([["d", "o", "g"], ["o", "g", "d"], ["d", "g", "o"]], "dog")` should return `[[0, 0], [0, 2]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_word([["d", "o", "g"], ["o", "g", "d"], ["d", "g", "o"]], "dog"), [[0, 0], [0, 2]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_word([["h", "i", "s", "h"], ["i", "s", "f", "s"], ["f", "s", "i", "i"], ["s", "h", "i", "f"]], "fish")` should return `[[3, 3], [0, 3]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_word([["h", "i", "s", "h"], ["i", "s", "f", "s"], ["f", "s", "i", "i"], ["s", "h", "i", "f"]], "fish"), [[3, 3], [0, 3]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_word([["f", "x", "o", "x"], ["o", "x", "o", "f"], ["f", "o", "f", "x"], ["f", "x", "x", "o"]], "fox")` should return `[[1, 3], [1, 1]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_word([["f", "x", "o", "x"], ["o", "x", "o", "f"], ["f", "o", "f", "x"], ["f", "x", "x", "o"]], "fox"), [[1, 3], [1, 1]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_word(matrix, word):
|
||||
|
||||
return matrix
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_word(matrix, word):
|
||||
rows = len(matrix)
|
||||
cols = len(matrix[0])
|
||||
length = len(word)
|
||||
|
||||
directions = [
|
||||
(0, 1),
|
||||
(0, -1),
|
||||
(1, 0),
|
||||
(-1, 0)
|
||||
]
|
||||
|
||||
for r in range(rows):
|
||||
for c in range(cols):
|
||||
for dr, dc in directions:
|
||||
match = True
|
||||
for i in range(length):
|
||||
nr = r + dr * i
|
||||
nc = c + dc * i
|
||||
if nr < 0 or nr >= rows or nc < 0 or nc >= cols or matrix[nr][nc] != word[i]:
|
||||
match = False
|
||||
break
|
||||
if match:
|
||||
return [[r, c], [r + dr * (length - 1), c + dc * (length - 1)]]
|
||||
```
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c7e
|
||||
title: "Challenge 92: Extension Extractor"
|
||||
challengeType: 29
|
||||
dashedName: challenge-92
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a filename, return the extension of the file.
|
||||
|
||||
- The extension is the part of the filename that comes after the last period (`.`).
|
||||
- If the filename does not contain a period or ends with a period, return `"none"`.
|
||||
- The extension should be returned as-is, preserving case.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_extension("document.txt")` should return `"txt"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension("document.txt"), "txt")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_extension("README")` should return `"none"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension("README"), "none")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_extension("image.PNG")` should return `"PNG"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension("image.PNG"), "PNG")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_extension(".gitignore")` should return `"gitignore"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension(".gitignore"), "gitignore")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_extension("archive.tar.gz")` should return `"gz"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension("archive.tar.gz"), "gz")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_extension("final.draft.")` should return `"none"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_extension("final.draft."), "none")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_extension(filename):
|
||||
|
||||
return filename
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_extension(filename):
|
||||
last_dot = filename.rfind('.')
|
||||
|
||||
if last_dot == -1 or last_dot == len(filename) - 1:
|
||||
return "none"
|
||||
|
||||
return filename[last_dot + 1:]
|
||||
```
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c7f
|
||||
title: "Challenge 93: Vowels and Consonants"
|
||||
challengeType: 29
|
||||
dashedName: challenge-93
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return an array with the number of vowels and number of consonants in the string.
|
||||
|
||||
- Vowels consist of `a`, `e`, `i`, `o`, `u` in any case.
|
||||
- Consonants consist of all other letters in any case.
|
||||
- Ignore any non-letter characters.
|
||||
|
||||
For example, given `"Hello World"`, return `[3, 7]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`count("Hello World")` should return `[3, 7]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("Hello World"), [3, 7])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count("JavaScript")` should return `[3, 7]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("JavaScript"), [3, 7])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count("Python")` should return `[1, 5]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("Python"), [1, 5])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count("freeCodeCamp")` should return `[5, 7]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("freeCodeCamp"), [5, 7])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count("Hello, World!")` should return `[3, 7]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("Hello World"), [3, 7])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count("The quick brown fox jumps over the lazy dog.")` should return `[11, 24]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count("Hello World"), [3, 7])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def count(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def count(s):
|
||||
vowels = "aeiou"
|
||||
consonants = "bcdfghjklmnpqrstvwxyz"
|
||||
v = 0
|
||||
c = 0
|
||||
|
||||
for char in s:
|
||||
ch = char.lower()
|
||||
if ch in vowels:
|
||||
v += 1
|
||||
if ch in consonants:
|
||||
c += 1
|
||||
|
||||
return [v, c]
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c80
|
||||
title: "Challenge 94: Email Signature Generator"
|
||||
challengeType: 29
|
||||
dashedName: challenge-94
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given strings for a person's name, title, and company, return an email signature as a single string using the following rules:
|
||||
|
||||
- The name should appear first, preceded by a prefix that depends on the first letter of the name. For names starting with (case-insensitive):
|
||||
- `A-I`: Use `>>` as the prefix.
|
||||
- `J-R`: Use `--` as the prefix.
|
||||
- `S-Z`: Use `::` as the prefix.
|
||||
- A comma and space (`, `) should follow the name.
|
||||
- The title and company should follow the comma and space, separated by `" at "` (with spaces around it).
|
||||
|
||||
For example, given `"Quinn Waverly"`, `"Founder and CEO"`, and `"TechCo"` return `"--Quinn Waverly, Founder and CEO at TechCo"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`generate_signature("Quinn Waverly", "Founder and CEO", "TechCo")` should return `"--Quinn Waverly, Founder and CEO at TechCo"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_signature("Quinn Waverly", "Founder and CEO", "TechCo"), "--Quinn Waverly, Founder and CEO at TechCo")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_signature("Alice Reed", "Engineer", "TechCo")` should return `">>Alice Reed, Engineer at TechCo"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_signature("Alice Reed", "Engineer", "TechCo"), ">>Alice Reed, Engineer at TechCo")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_signature("Tina Vaughn", "Developer", "example.com")` should return `"::Tina Vaughn, Developer at example.com"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_signature("Tina Vaughn", "Developer", "example.com"), "::Tina Vaughn, Developer at example.com")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_signature("B. B.", "Product Tester", "AcmeCorp")` should return `">>B. B., Product Tester at AcmeCorp"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_signature("B. B.", "Product Tester", "AcmeCorp"), ">>B. B., Product Tester at AcmeCorp")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`generate_signature("windstorm", "Cloud Architect", "Atmospheronics")` should return `"::windstorm, Cloud Architect at Atmospheronics"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(generate_signature("windstorm", "Cloud Architect", "Atmospheronics"), "::windstorm, Cloud Architect at Atmospheronics")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def generate_signature(name, title, company):
|
||||
|
||||
return name
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def generate_signature(name, title, company):
|
||||
first_letter = name[0].upper()
|
||||
if first_letter in "ABCDEFGHI":
|
||||
prefix = ">>"
|
||||
elif first_letter in "JKLMNOPQR":
|
||||
prefix = "--"
|
||||
else:
|
||||
prefix = "::"
|
||||
|
||||
return f"{prefix}{name}, {title} at {company}"
|
||||
```
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c81
|
||||
title: "Challenge 95: Array Shift"
|
||||
challengeType: 29
|
||||
dashedName: challenge-95
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array and an integer representing how many positions to shift the array, return the shifted array.
|
||||
|
||||
- A positive integer shifts the array to the left.
|
||||
- A negative integer shifts the array to the right.
|
||||
- The shift wraps around the array.
|
||||
|
||||
For example, given `[1, 2, 3]` and `1`, shift the array 1 to the left, returning `[2, 3, 1]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`shift_array([1, 2, 3], 1)` should return `[2, 3, 1]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(shift_array([1, 2, 3], 1), [2, 3, 1])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`shift_array([1, 2, 3], -1)` should return `[3, 1, 2]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(shift_array([1, 2, 3], -1), [3, 1, 2])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`shift_array(["alpha", "bravo", "charlie"], 5)` should return `["charlie", "alpha", "bravo"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(shift_array(["alpha", "bravo", "charlie"], 5), ["charlie", "alpha", "bravo"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`shift_array(["alpha", "bravo", "charlie"], -11)` should return `["bravo", "charlie", "alpha"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(shift_array(["alpha", "bravo", "charlie"], -11), ["bravo", "charlie", "alpha"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`shift_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15)` should return `[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(shift_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 15), [5, 6, 7, 8, 9, 0, 1, 2, 3, 4])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def shift_array(arr, n):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def shift_array(arr, n):
|
||||
length = len(arr)
|
||||
n = n % length
|
||||
if n < 0:
|
||||
n += length
|
||||
|
||||
return arr[n:] + arr[:n]
|
||||
```
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c82
|
||||
title: "Challenge 96: Is It the Weekend?"
|
||||
challengeType: 29
|
||||
dashedName: challenge-96
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a date in the format `"YYYY-MM-DD"`, return the number of days left until the weekend.
|
||||
|
||||
- The weekend starts on Saturday.
|
||||
- If the given date is Saturday or Sunday, return `"It's the weekend!"`.
|
||||
- Otherwise, return `"X days until the weekend."`, where `X` is the number of days until Saturday.
|
||||
- If `X` is `1`, use `"day"` (singular) instead of `"days"` (plural).
|
||||
- Make sure the calculation ignores your local timezone.
|
||||
|
||||
# --hints--
|
||||
|
||||
`days_until_weekend("2025-11-14")` should return `"1 day until the weekend."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2025-11-14"), "1 day until the weekend.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`days_until_weekend("2025-01-01")` should return `"3 days until the weekend."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2025-01-01"), "3 days until the weekend.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`days_until_weekend("2025-12-06")` should return `"It's the weekend!"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2025-12-06"), "It's the weekend!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`days_until_weekend("2026-01-27")` should return `"4 days until the weekend."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2026-01-27"), "4 days until the weekend.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`days_until_weekend("2026-09-07")` should return `"5 days until the weekend."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2026-09-07"), "5 days until the weekend.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`days_until_weekend("2026-11-29")` should return `"It's the weekend!"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(days_until_weekend("2026-11-29"), "It's the weekend!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def days_until_weekend(date_string):
|
||||
|
||||
return date_string
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
from datetime import datetime
|
||||
|
||||
def days_until_weekend(date_string):
|
||||
date = datetime.strptime(date_string, "%Y-%m-%d").date()
|
||||
day_of_week = date.weekday()
|
||||
|
||||
if day_of_week == 5 or day_of_week == 6:
|
||||
return "It's the weekend!"
|
||||
|
||||
days_until_saturday = 5 - day_of_week
|
||||
return f"{days_until_saturday} day{'s' if days_until_saturday != 1 else ''} until the weekend."
|
||||
```
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c83
|
||||
title: "Challenge 97: GCD"
|
||||
challengeType: 29
|
||||
dashedName: challenge-97
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two positive integers, return their greatest common divisor (GCD).
|
||||
|
||||
- The GCD of two integers is the largest number that divides evenly into both numbers without leaving a remainder.
|
||||
|
||||
For example, the divisors of `4` are `1`, `2`, and `4`. The divisors of `6` are `1`, `2`, `3`, and `6`. So given `4` and `6`, return `2`, the largest number that appears in both sets of divisors.
|
||||
|
||||
# --hints--
|
||||
|
||||
`gcd(4, 6)` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(gcd(4, 6), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`gcd(20, 15)` should return `5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(gcd(20, 15), 5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`gcd(13, 17)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(gcd(13, 17), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`gcd(654, 456)` should return `6`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(gcd(654, 456), 6)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`gcd(3456, 4320)` should return `864`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(gcd(3456, 4320), 864)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def gcd(x, y):
|
||||
|
||||
return x
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def gcd(x, y):
|
||||
while y != 0:
|
||||
x, y = y, x % y
|
||||
return x
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c84
|
||||
title: "Challenge 98: Rectangle Count"
|
||||
challengeType: 29
|
||||
dashedName: challenge-98
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two positive integers representing the width and height of a rectangle, determine how many rectangles can fit in the given one.
|
||||
|
||||
- Only count rectangles with integer width and height.
|
||||
|
||||
For example, given `1` and `3`, return `6`. Three 1x1 rectangles, two 1x2 rectangles, and one 1x3 rectangle.
|
||||
|
||||
# --hints--
|
||||
|
||||
`count_rectangles(1, 3)` should return `6`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_rectangles(1, 3), 6)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_rectangles(3, 2)` should return `18`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_rectangles(3, 2), 18)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_rectangles(1, 2)` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_rectangles(1, 2), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_rectangles(5, 4)` should return `150`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_rectangles(5, 4), 150)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_rectangles(11, 19)` should return `12540`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_rectangles(11, 19), 12540)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def count_rectangles(width, height):
|
||||
|
||||
return width
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def count_rectangles(width, height):
|
||||
total = 0
|
||||
for w in range(1, width + 1):
|
||||
for h in range(1, height + 1):
|
||||
total += (width - w + 1) * (height - h + 1)
|
||||
return total
|
||||
```
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
---
|
||||
id: 68f6587287ad1f4ad39b0c85
|
||||
title: "Challenge 99: Fingerprint Test"
|
||||
challengeType: 29
|
||||
dashedName: challenge-99
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two strings representing fingerprints, determine if they are a match using the following rules:
|
||||
|
||||
- Each fingerprint will consist only of lowercase letters (`a-z`).
|
||||
- Two fingerprints are considered a match if:
|
||||
- They are the same length.
|
||||
- The number of differing characters does not exceed 10% of the fingerprint length.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_match("helloworld", "helloworld")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("helloworld", "helloworld"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_match("helloworld", "helloworlds")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("helloworld", "helloworlds"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_match("helloworld", "jelloworld")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("helloworld", "jelloworld"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_match("thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_match("theslickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthehazydog")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("theslickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthehazydog"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_match("thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthehazycat")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_match("thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthehazycat"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_match(fingerprint_a, fingerprint_b):
|
||||
|
||||
return fingerprint_a
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_match(fingerprint_a, fingerprint_b):
|
||||
if len(fingerprint_a) != len(fingerprint_b):
|
||||
return False
|
||||
|
||||
mismatches = 0
|
||||
|
||||
for c1, c2 in zip(fingerprint_a, fingerprint_b):
|
||||
if c1 != c2:
|
||||
mismatches += 1
|
||||
if mismatches > len(fingerprint_a) // 10:
|
||||
return False
|
||||
|
||||
return True
|
||||
```
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 68ffb91507a5b645769328c3
|
||||
title: "Challenge 100: 100 Characters"
|
||||
challengeType: 29
|
||||
dashedName: challenge-100
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Welcome to the 100th Daily Coding Challenge!
|
||||
|
||||
Given a string, repeat its characters until the result is exactly 100 characters long. If your repetitions go over 100 characters, trim the extra so it's exactly 100.
|
||||
|
||||
# --hints--
|
||||
|
||||
`one_hundred("One hundred ")` should return `"One hundred One hundred One hundred One hundred One hundred One hundred One hundred One hundred One "`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(one_hundred("One hundred "), "One hundred One hundred One hundred One hundred One hundred One hundred One hundred One hundred One ")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`one_hundred("freeCodeCamp ")` should return `"freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeC"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(one_hundred("freeCodeCamp "), "freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeCamp freeCodeC")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`one_hundred("daily challenges ")` should return `"daily challenges daily challenges daily challenges daily challenges daily challenges daily challenge"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(one_hundred("daily challenges "), "daily challenges daily challenges daily challenges daily challenges daily challenges daily challenge")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`one_hundred("!")` should return `"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(one_hundred("!"), "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")`)
|
||||
}})
|
||||
```
|
||||
|
||||
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def one_hundred(chars):
|
||||
|
||||
return chars
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def one_hundred(chars):
|
||||
result = ""
|
||||
i = 0
|
||||
|
||||
while len(result) < 100:
|
||||
result += chars[i % len(chars)]
|
||||
i += 1
|
||||
|
||||
return result[:100]
|
||||
```
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user