Skip to content

Instantly share code, notes, and snippets.

@mul14
Last active March 1, 2024 01:12
Show Gist options
  • Save mul14/3698eadf2511b441d4b6a8132b4f64a9 to your computer and use it in GitHub Desktop.
Save mul14/3698eadf2511b441d4b6a8132b4f64a9 to your computer and use it in GitHub Desktop.
Someone give me some coding test on CoderByte. Here are the questions and my answers.
/*
Have the function
ArithGeo(arr)
take the array of numbers stored in
arr
and return the string
"Arithmetic"
if the sequence follows an arithmetic pattern or return
"Geometric"
if it follows a geometric pattern. If the sequence doesn't follow either pattern return
-1
. An arithmetic sequence is one where the difference between each of the numbers is consistent,
where as in a geometric sequence, each term after the first is multiplied by some constant or common ratio.
Arithmetic example: [2, 4, 6, 8] and
Geometric example: [2, 6, 18, 54].
Negative numbers may be entered as parameters,
0 will not be entered, and no array will contain all the same elements.
*/
function isArithmetic(arr) {
let output = true
const delta = arr[0] - arr[1]
for (let i = 0; i < arr.length -1; i++) {
if (delta !== arr[i] - arr[i + 1]) {
output = false
break
}
}
return output
}
function isGeometric(arr) {
let output = true
const delta = arr[1] / arr[0]
for (let i = 0; i < arr.length -1; i++) {
if (delta !== arr[i + 1] / arr[i]) {
output = false
break
}
}
return output
}
function ArithGeo(arr) {
// 0 will not be entered as param.
if (arr.includes(0)) {
throw new Error('0 is not allowed')
}
// No array will contain all the same elements.
arr = Array.from(new Set(arr));
if (isArithmetic(arr)) return 'Arithmetic'
if (isGeometric(arr)) return 'Geometric'
return undefined
}
console.log(ArithGeo([2, 4, 6, 8])) // Arithmetic
console.log(ArithGeo([2, 6, 18, 54])) // Geometric
console.log(ArithGeo([2, 4, 6, 10])) // undefined
console.log(ArithGeo([0, 1, 2])) // Error
/*
Have the function
ArithGeo(arr)
take the array of numbers stored in
arr
and return the string
"Arithmetic"
if the sequence follows an arithmetic pattern or return
"Geometric"
if it follows a geometric pattern. If the sequence doesn't follow either pattern return
-1
. An arithmetic sequence is one where the difference between each of the numbers is consistent,
where as in a geometric sequence, each term after the first is multiplied by some constant or common ratio.
Arithmetic example: [2, 4, 6, 8] and
Geometric example: [2, 6, 18, 54].
Negative numbers may be entered as parameters,
0 will not be entered, and no array will contain all the same elements.
*/
function isArithmetic(arr) {
let output = true
const delta = arr[0] - arr[1]
for (let i = 0; i < arr.length -1; i++) {
if (delta !== arr[i] - arr[i + 1]) {
output = false
break
}
}
return output
}
function isGeometric(arr) {
let output = true
const delta = arr[1] / arr[0]
for (let i = 0; i < arr.length -1; i++) {
if (delta !== arr[i + 1] / arr[i]) {
output = false
break
}
}
return output
}
function ArithGeo(arr) {
// 0 will not be entered as param.
if (arr.includes(0)) {
throw new Error('0 is not allowed')
}
// No array will contain all the same elements.
arr = Array.from(new Set(arr));
if (isArithmetic(arr)) return 'Arithmetic'
if (isGeometric(arr)) return 'Geometric'
return undefined
}
console.log(ArithGeo([2, 4, 6, 8])) // Arithmetic
console.log(ArithGeo([2, 6, 18, 54])) // Geometric
console.log(ArithGeo([2, 4, 6, 10])) // undefined
console.log(ArithGeo([0, 1, 2])) // Error
/*
Have the function
DashInsertII(str)
insert dashes ('-') between each two odd numbers and insert asterisks ('*') between each two even numbers in
str
. For example: if
str
is
4546793
the output should be
454*67-9-3
. Don't count zero as an odd or even number.
*/
const isEven = n => n % 2 === 0
const isOdd = n => !isEven(n)
function DashInsertII(str) {
str = (str + '').split('')
let result = ''
for (let i = 0; i < str.length; i++) {
result += str[i]
if (str[i] === '0' && str[i + 1] === '0') continue
if (isEven(str[i]) && isEven(str[i + 1])) result += '*'
if (isOdd(str[i]) && isOdd(str[i + 1])) result += '-'
}
// Clean-up trailing - or *
result = result.replace(/[-|*]$/, '')
return result
}
console.log(DashInsertII('4546793')) // Output: 454*67-9-3
console.log(DashInsertII('45467930022')) // Output: 454*67-9-300*2*2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment