Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Last active June 30, 2018 19:14
Show Gist options
  • Save huseyinyilmaz/efe86ee8e2b24a985b74e9e3c2773b22 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/efe86ee8e2b24a985b74e9e3c2773b22 to your computer and use it in GitHub Desktop.
Programming questions for middleschoolers.

Questions

Find shortest Name

Given name last name objects find the person with shortest name.

example

names = [{'name': 'huseyin', 'last_name': 'yilmaz'},
         {'name': 'sila', 'last_name': 'yilmaz'}]
get_shortest(names)
>>> {'name': 'sila', 'last_name': 'yilmaz'}

Reverse names

Given dog owner pairs return owner-> dogs pairs

example

get_dogs({'tommy': 'huseyin', 'oyuncu': 'huseyin', 'kara': 'sila'})
>>> {'huseyin': ['tommy', 'oyuncu'], 'sila': ['kara']}

find_shots

Given list of dog profiles. write a function that find out which dog needs a spesific shots:

example

dogs = [{'name': 'tommy': , 'shots': ['a','c']},
        {'name': 'oyuncu', 'shots': ['a','b']},
        {'name': 'kara', 'shots': ['b','c']}]
find_shots(dogs, 'a') -> ['kara']
find_shots(dogs, 'b') -> ['tommy']
find_shots(dogs, 'c') -> ['oyuncu']

Old Questions

list_remove_idx

remove element at the given index from the list. (return a new list.)

example

list_remove_idx(['a', 'b', 'c', 'd'], 2) -> ['a', 'b', 'd']
list_remove_idx(['a', 'b', 'c', 'd'], 5) -> ['a', 'b', 'c', 'd']

list_remove

remove all instances of given element from list.

example

list_remove_idx(['a', 'b', 'c', 'd', 'a'], 'a') -> ['b', 'c', 'd']
list_remove_idx(['a', 'b', 'c', 'd', 'a'], 'e') -> ['a', 'b', 'c', 'd', 'a']

list_insert

insert given element on given index. After operation is done, given element has to be on given index.

example

list_insert_idx(['a', 'b', 'c', 'd'], 'x', 2) -> ['a', 'b', 'x', 'c', 'd']

find_element

return the index of an element from given list. if element does not exist in the list return None

example

find_element(['a', 'b', 'c', 'd'], 'c') -> 2
find_element(['a', 'b', 'c', 'd'], 'd') -> 3
find_element(['a', 'b', 'c', 'd'], 'b') -> 1
find_element(['a', 'b', 'c', 'd'], 'z') -> None

even_number_count

return number of even numbers in a list

example

even_count([1,2,3,4]) -> 2
even_count([2,3,4,5,6]) -> 3
even_count([2]) -> 1

is_positive

return true if sum of all elements in a list is positive

example

even_count([1,2, -4]) -> False
even_count([2,3,-3]) -> True
even_count([0]) -> False

add_distance

Add index of the list to values and return new list.

example

>>> add_distance([5,4,3,9])
[5+0, 4+1, 3+2, 9+3]
[5, 5, 5, 12]

reverse a list

Reverse a given list:

example

>>> rev([1,2,3])
[3,2,1]

2-sum

get a list and return all element tuples that has sum of 0

example

>>> two_sum([1,2,3,4,5, -3 , -5])
[(3,-3), (5, -5)]

Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

example

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Jewels and Stones

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”.

example 1

Input: J = "aA", S = "aAAbbbb"
Output: 3

example 2

Input: J = "z", S = "ZZ"
Output: 0

Note

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

example 1

Input: "UD"
Output: true

example 2

Input: "LL"
Output: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment