Happy International Programmers' Day!
Did you know?
International Programmers’ Day on the 256th day of the year because “256” (28) is the number of distinct values that can be represented with an 8-bit byte, and 256 is the highest power of 2 that is less than 365, the number of days in a year!
Today we’re celebrating the programmers behind the scenes of computers, technology, and software that make our modern world go around. They solve problems with clever code, cloud security solutions, and intense development projects!
So we’d like to challenge them slightly by providing some fun coding puzzles!
JavaScript Challenge
Caesar’s Cipher
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar’s cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Create a function that takes a string s (text to be encrypted) and an integer k (the rotation factor). It should return an encrypted string.
Examples
caesarCipher("middle-Outz", 2) ➞ "okffng-Qwvb"
// m -> o
// i -> k
// d -> f
// d -> f
// l -> n
// e -> g
// - -
// O -> Q
// u -> w
// t -> v
// z -> b
caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5)
➞ "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj"
caesarCipher("A friend in need is a friend indeed", 20)
➞ "U zlcyhx ch hyyx cm u zlcyhx chxyyx"
Python Challenge
What Gives a Bad Mood?
The factors said to have the greatest impact on someone’s mood are: weather, meals, and sleep. Your task is, given a list of sublists of different values for: Mood, Weather, Meals, and Sleep, determine which other variable has had the greatest impact on the mood.
Examples
greatest_impact([
[1, 1, 3, 10],
[1, 1, 3, 10],
[1, 1, 3, 10]
]) ➞ "Weather"
# As it was always low but all others were high.
greatest_impact([
[10, 10, 3, 10],
[10, 10, 3, 10],
[10, 10, 3, 10]
]) ➞ "Nothing"
# As all were always high.
Notes
The mood will always go from 1 to 10, the weather will always go from 1 to 10, the meals will always go from 1 to 3, and the sleep will always go from 1 to 10. In cases of mood and weather, 1 is negative and 10 is positive. All sublists values will be in the order [Mood, Weather, Meals, Sleep]
C++ Challenge
Vampire Numbers
A Vampire Number is a positive integer greater than 99, that rearranged in all of its possible digits permutations, with every permutation being split into two parts, is equal to the product of at least one of its permutations.
- If the number has an even quantity of digits, left and right parts will have the same length in every permutation;
- If the number has an odd quantity of digits and at least three digits, the left and right parts will present different lengths for every possible permutation, alternating between them in the range +1 and -1.
Given a positive integer n, implement a function that returns the type of n as a string:
'Normal Number'
if n is lower than 100 or if no permutations return a product of their parts equal to n.
'Pseudovampire'
if n it is a Vampire with an odd quantity of digits.
'True Vampire'
if n it is a Vampire with an even quantity of digits.
Examples
isVampire(1260) ➞ "True Vampire"
// Has an even number of digits and is greater than 99)
// Permutations:
// 12 * 60 = 720
// 16 * 20 = 320
// 10 * 26 = 260
// 21 * 60 = 1260
isVampire(126) ➞ "Pseudovampire"
// Has an odd number of digits and is greater than 99
// Permutations:
// 12 * 6 = 72
// 1 * 26 = 26
// 21 * 6 = 126
isVampire(67) ➞ "Normal Number"
// Is lower than 100
// Permutations:
// 6 * 7 = 7 * 6 = 42
Notes
Trivially, a number from 1 to 99 is a Normal Number by the definitions: a single-digit number can’t be split into two parts, and the product of the permutated two digits of a number will always be lower than the number itself.