Skip to content

Instantly share code, notes, and snippets.

@ultimania92
Last active March 10, 2022 22:07
Show Gist options
  • Save ultimania92/70885e9fecf867fdbf3217f16c209b99 to your computer and use it in GitHub Desktop.
Save ultimania92/70885e9fecf867fdbf3217f16c209b99 to your computer and use it in GitHub Desktop.
""" Task Description:
Please write a class in the language of your choice that contains the following two public methods:
aboveBelow
accepts two arguments
An unsorted collection of integers (the list)
an integer (the comparison value)
returns a hash/object/map/etc. with the keys "above" and "below" with the corresponding count of integers from the list that are above or below the comparison value
Example usage:
input: [1, 5, 2, 1, 10], 6
output: { "above": 1, "below": 4 }
stringRotation
accepts two arguments
a string (the original string)
a positive integer (the rotation amount)
returns a new string, rotating the characters in the original string to the right by the rotation amount and have the overflow appear at the beginning
Example usage:
input: "MyString", 2
output: "ngMyStri"
"""
class simpleTasks:
@staticmethod
def aboveBelow(values, breakpoint):
"""
Returns a hash/dict/object dictating how many values are 'above' or 'below' the breakpoint value.
Example usage:
- Input: [1, 5, 2, 1, 10], 6
- Output: { "above": 1, "below": 4 }
:param values: Any list, sorted or unsorted of integers. It is faster and more performant to just iterate on values
once each and see if they are above or below breakpoints.
:param breakpoint: The value to compare against
:return: A dict with the counts above and below breakpoint.
:raises TypeError: Raised if any non-integer values are in the list due to 'non-comparable types'.
There's plenty of ways to sanity check here (ignore non-integer/non 'integer-convertible' values;
catch the inevitable TypeError on a bad cross-type comparison like str() and int())
"""
output = {'above': 0, 'below': 0}
for value in values:
if value < breakpoint:
output['below'] += 1
elif value > breakpoint:
output['above'] += 1
return output
@staticmethod
def stringRotation(inputString, rotAmount=0):
"""
'Rotates' a string by 'rotAmount' characters
Example Usage:
- Input: "MyString", 2
- Output: "ngMyStri"
:param inputString: The string to rotate 'from the right'.
:param rotAmount: A positive integer denoting how many characters to rotate from the right.
:return: A new string with rotAmount characters rotated from the end of the string to the front by slice.
:raises ValueError: Raised when rotAmount exceeds len(inputString) or is otherwise a non-positive integer
:raises TypeError: Raised when rotAmount is a float. Spec doesn't specify accepting floats as a value here,
BUT this could gracefully see if a float can convert to an int with float.is_integer() before raising
an error.
"""
if not isinstance(rotAmount, int):
raise TypeError("rotAmount value expects an integer in range {1, len(inputString}; got {}".format(rotAmount))
if 0 < rotAmount <= len(inputString):
return inputString[rotAmount * -1:] + inputString[:rotAmount * -1]
else:
raise ValueError("rotAmount must be an integer in range {1, len(inputString}; got {}".format(rotAmount))
foo = simpleTasks.aboveBelow([1, 5, 2, 1, 10], 6)
bar = simpleTasks.stringRotation("MyString", 2)
print(foo, bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment