Skip to content

Instantly share code, notes, and snippets.

@yoosuf
Last active March 19, 2023 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoosuf/f8fd94ba0d5740f49dc48a59b3b2a288 to your computer and use it in GitHub Desktop.
Save yoosuf/f8fd94ba0d5740f49dc48a59b3b2a288 to your computer and use it in GitHub Desktop.
Scopic Live Code Test : You're given an array that shows the relative positions of the letters of a word. Your task is to find the word hidden behind the array.
def find_word(arr):
word = ''
temp = {}
# Build a dictionary where the key is the letter and the value is the letter that follows it
for item in arr:
chars = item.split('>')
temp[chars[0]] = chars[1]
# Start with the first letter and keep appending the next letter until the end of the word is reached
current = list(temp.keys())[0]
while current in temp:
word += current
current = temp[current]
# Append the last letter and return the word
word += current
return word
def find_word(arr)
word = ''
temp = {}
# Build a hash where the key is the letter and the value is the letter that follows it
arr.each do |item|
chars = item.split('>')
temp[chars[0]] = chars[1]
end
# Start with the first letter and keep appending the next letter until the end of the word is reached
current = temp.keys[0]
while temp.key?(current)
word += current
current = temp[current]
end
# Append the last letter and return the word
word += current
word
end
arr = ['S>P', 'P>A', 'A>I', 'I>N']
word = find_word(arr)
puts word # Output: SPAIN
package main
import (
"fmt"
)
func findWord(arr []string) string {
word := ""
temp := make(map[string]string)
// Build a map where the key is the letter and the value is the letter that follows it
for _, item := range arr {
chars := []rune(item)
temp[string(chars[0])] = string(chars[2])
}
// Start with the first letter and keep appending the next letter until the end of the word is reached
current := ""
for key := range temp {
current = key
break
}
for _, ok := temp[current]; ok; _, ok = temp[current] {
word += current
current = temp[current]
}
// Append the last letter and return the word
word += current
return word
}
func main() {
arr := []string{"S>P", "P>A", "A>I", "I>N"}
word := findWord(arr)
fmt.Println(word) // Output: SPAIN
}
function findWord(arr) {
let word = '';
const temp = {};
// Build an object where the key is the letter and the value is the letter that follows it
arr.forEach((item) => {
const [char1, , char2] = item.split('');
temp[char1] = char2;
});
// Start with the first letter and keep appending the next letter until the end of the word is reached
let current = Object.keys(temp)[0];
while (current in temp) {
word += current;
current = temp[current];
}
// Append the last letter and return the word
word += current;
return word;
}
<?php
function findWord($arr) {
$word = '';
$temp = [];
// Build an associative array where the key is the letter and the value is the letter that follows it
foreach($arr as $item) {
$chars = str_split($item);
$temp[$chars[0]] = $chars[2];
}
// Start with the first letter and keep appending the next letter until the end of the word is reached
$current = array_key_first($temp);
while(isset($temp[$current])) {
$word .= $current;
$current = $temp[$current];
}
// Append the last letter and return the word
$word .= $current;
return $word;
}
function findWord(arr: string[]): string {
let word = '';
const temp: Record<string, string> = {};
// Build an object where the key is the letter and the value is the letter that follows it
arr.forEach((item) => {
const [char1, , char2] = item.split('');
temp[char1] = char2;
});
// Start with the first letter and keep appending the next letter until the end of the word is reached
let current = Object.keys(temp)[0];
while (current in temp) {
word += current;
current = temp[current];
}
// Append the last letter and return the word
word += current;
return word;
}
You're given an array that shows the relative positions of the letters of a word. Your task is to find the word hidden behind the array. For example:
Input: ['S>P', 'P>A', 'A>I', 'I>N']
What it means:
S>P: S is followed by P
P>A: P is followed by A
A>I: A is followed by I
I>N: I is followed by N
Output: SPAIN
Note 1: the array can be given in any order. For example, the input ['I>N', 'A>I', 'P>A', 'S>P'] will produce the same output as the above example
Note 2: it is guaranteed that each letter in the output word is unique
Note 3: the middle character in each element doesn't change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment