Skip to content

Instantly share code, notes, and snippets.

# An avid hiker keeps meticulous records of their hikes. During the last hike that
# took exactly steps, for every step it was noted if it was an uphill, , or a
# downhill, step. Hikes always start and end at sea level, and each step up or down
# represents a unit change in altitude. We define the following terms:
# A mountain is a sequence of consecutive steps above sea level, starting with a step
# up from sea level and ending with a step down to sea level.
# A valley is a sequence of consecutive steps below sea level, starting with a step
# down from sea level and ending with a step up to sea level.
# Given the sequence of up and down steps during a hike, find and print the number
# There is a new mobile game that starts with consecutively numbered clouds.
# Some of the clouds are thunderheads and others are cumulus. The player can jump
# on any cumulus cloud having a number that is equal to the number of the current
# cloud plus 1 or 2. The player must avoid the thunderheads. Determine the minimum
# number of jumps it will take to jump from the starting postion to the last cloud.
# It is always possible to win the game.
# For each game, you will get an array of clouds numbered 0 if they are safe or 1
# if they must be avoided.
# Example
# You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order.
# Example
# Perform the following steps:
# i arr swap (indices)
# 0 [7, 1, 3, 2, 4, 5, 6] swap (0,3)
# 1 [2, 1, 3, 7, 4, 5, 6] swap (0,1)
@omokehinde
omokehinde / workbook.py
Created December 26, 2021 12:38
Hackerrank Lisa's workbook practice question
# Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters. Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it's located. The format of Lisa's book is as follows:
# There are chapters in Lisa's workbook, numbered from to .
# The chapter has problems, numbered from to .
# Each page can hold up to problems. Only a chapter's last page of exercises may contain fewer than problems.
# Each new chapter starts on a new page, so a page will never contain problems from more than one chapter.
# The page number indexing starts at .
# Given the details for Lisa's workbook, can you count its number of special problems?
# Example
@omokehinde
omokehinde / quiz.rb
Created January 30, 2022 00:36
This gist demonstrates how to create an interactive quiz that asks questions from the terminal
class Question
attr_accessor :prompt, :answer
def initialize(prompt, answer)
@prompt = prompt
@answer = answer
end
end
p1 = "What color are apples?\n(a)red (b)purple (c) orange"
p2 = "What color are bananas?\n(a)pink (b)red (c) yellow"
@omokehinde
omokehinde / acmTeam.py
Last active January 31, 2022 23:19
HackerRank ACM ICPC Team challenge
# There are a number of people who will be attending ACM-ICPC World Finals. Each of them may be well versed in a number of topics. Given a list of topics known by each attendee, presented as binary strings, determine the maximum number of topics a 2-person team can know. Each subject has a column in the binary string, and a '1' means the subject is known while '0' means it is not. Also determine the number of teams that know the maximum number of topics. Return an integer array with two elements. The first is the maximum number of topics known, and the second is the number of teams that know that number of topics.
# Example
# The attendee data is aligned for clarity below:
# 10101
# 11110
@omokehinde
omokehinde / beautifulTriplets.py
Last active February 1, 2022 23:28
HackerRank Beautiful Triplets challenge
# Given a sequence of integers , a triplet is beautiful if:
# Given an increasing sequenc of integers and the value of , count the number of beautiful triplets in the sequence.
# Example
# There are three beautiful triplets, by index: . To test the first triplet, and .
# Function Description
@omokehinde
omokehinde / absolutePermutation.py
Created February 10, 2022 00:44
HackerRank Absolute Permutation challenge
# We define to be a permutation of the first natural numbers in the range . Let denote the value at position in permutation using -based indexing.
# is considered to be an absolute permutation if holds true for every .
# Given and , print the lexicographically smallest absolute permutation . If no absolute permutation exists, print -1.
# Example
# Create an array of elements from to , . Using based indexing, create a permutation where every . It can be rearranged to so that all of the absolute differences equal :
@omokehinde
omokehinde / kaprekarNumbers.py
Last active February 10, 2022 23:46
HackerRank Modified Kaprekar Numbers challenge
# A modified Kaprekar number is a positive whole number with a special property. If you square it, then split the number into two integers and sum those integers, you have the same value you started with.
# Consider a positive whole number with digits. We square to arrive at a number that is either digits long or digits long. Split the string representation of the square into two parts, and . The right hand part, must be digits long. The left is the remaining substring. Convert those two substrings back to integers, add them and see if you get .
# Example
# First calculate that . Split that into two strings and convert them back to integers and . Test , so this is not a modified Kaprekar number. If , still , and . This gives us , the original .
def StringReduction(str)
# code goes here
hash = {"ab":"c", "ac":"b", "bc":"a",
"ba":"c", "ca":"b", "cb":"a"}
count = 0
while str != str[0]*str.length
if hash.has_key? :"#{str[count..count+1]}"
str = str.sub(str[count..count+1], hash[:"#{str[count..count+1]}"])
count = 0