# 100 Days of Code Day - 17 More Algo's please

Hello! My name is Rick and I am in search for gig as a developer :)

This past December I finished up a year long study with a full-stack school in NYC called Codeimmersives. We explored HTML, CSS, JavaScript, and how to build applications using the MERN stack. I learned about class-based components, passing props around (prop-drilling) and utilizing the component lifecycle(componentWillMount, componentDidMount, etc). After learning class based components, we moved on to functional based components using hooks, custom hooks, and different ways to manage state, such as the useContext and Redux APIs. (This is just a brief overview of the technologies I studied over the past year). Currently I am enrolled at devCodeCamp learning Python and solving problems. I am documenting my journey by sharing these blog posts. Please help me out by dropping a comment below on what you think!

Today was a good day. I had plenty of rest the night before and hit the gym first thing in the morning. From 10am - 6pm I solved problems in python (with the exception of an hour for lunch and a few small breaks here and there). It really helps to sleep and exercise; it felt like I was amped up all day long!

Below is my work :)

```
# Steps of the software development process:
# 1. Based on a given starting point (feature, task, code block, etc.), what is the
# expected end result?
# 2. What are the written-out steps to go from point A to point B? You need to
# solve the problem before you begin coding it.
# 3. Implementation (coding it out, researching)
# 4. Test and debug code (run code with breakpoint, unit test, etc.)
# 5. Refactor if necessary. Test again. This continues until functionality is
# solidified.
# The above steps should be rinse and repeated for every single problem you
# encounter. Ignoring these steps or straying from them will result in the long way
# around to solving a problem or even possibly never solving the problem.
# To be a good problem solver, it is important to be able to break problems down.
# One way to go about this is to write out the steps it will take to solve the problem.
# These steps are written down in English in a manner that are easily explainable to
# someone who may not be technical. The idea is that in order to code something out,
# you first need to have a good understanding of what it is you are attempting to
# solve. For each of the problems below, write out the steps it will take to go about
# solving the problem. Then code it out and test!
# You may jump around in these problems. If you get stuck on one problem, begin
# working on another. If you get stuck on that new problem, go back to working on
# the previous one.
# The use cases below are just examples to give you a better idea of what might be
# passed into the method or what might be outputted from the method. You shouldn’t
# be coding exactly to these examples, but rather, be flexible to handle any data of
# that data type.
# Whiteboard Challenges
# 1. Given an array of integers, return indices of the two numbers such that they
# add up to a specific target. You may assume that each input would
# have exactly one solution, and you may not use the same element twice.
# a. Use Case:
# i. Given numbers in an array: [5, 17, 77, 50]
# ii. Target: 55
def find_indices(list, target):
# check each index of possible pairs to see if they add to target number
for i in range(0,len(list)):
for j in range(i,len(list)):
if list[i] + list[j] == target:
# expected end result is two numbers
return [i,j]
print(find_indices([2,3,4,22,4,6], 28))
# 2. Given a number, return the reciprocal of the reverse of the original number,
# as a double.
# a. Use case: If given 17, return 0.01408 (1/71)
def recip_reverse(num):
# make string
num = str(num)
# reverse string
num = num[::-1]
# reciprocal
num = 1/int(num)
# format 5 decimal places to right
num = "{:.5f}".format(num)
return num
print(recip_reverse(17))
# 3. A briefcase has a four-digit rolling-lock. Each digit is a number from 0-9 that
# can be rolled either forwards or backwards. Write a method that returns the
# smallest number of turns it takes to transform the lock from current
# combination to the target combination. One turn is equivalent to rolling a
# number forwards or backwards by one.
# a. Use case:
# i. Current lock: 3893
# ii. Target lock: 5296
def to_str(char):
return str(char)
def how_many_turns(current_lock, target_lock):
# parse to str for iteration
current_lock = str(current_lock)
target_lock = str(target_lock)
# est count var to keep count of distance
count = 0
for i in range(0,len(current_lock)):
# make sure to continue if same
if current_lock[i] == target_lock[i]:
continue
num1 = int(current_lock[i])
num2 = int(target_lock[i])
# little math to compensate for index 0-9
if num1 < num2:
count += num2 - num1
else:
count += (num2 + 10) - num1
# return count
return count
print(how_many_turns(3893, 5296))
# 4. Given a list of integers, return a bool that represents whether or not all
# integers in the list can form a sequence of incrementing integers
# a. Use case:
# i. {5, 7, 3, 8, 6} false (no 4 to complete the sequence)
# ii. {17, 15, 20, 19, 21, 16, 18} true
def can_sort_by_increments(list_of_nums):
list_of_nums.sort()
for i in range(1,len(list_of_nums)):
num1 = list_of_nums[i - 1]
num2 = list_of_nums[i]
if num2 != num1 + 1:
return False
return True
print(can_sort_by_increments([5, 7, 3, 8, 6]))
# 5. Create a method that takes an array of positive and negative numbers.
# Return an array where the first element is the count of the positive numbers
# and the second element is the sum of negative numbers.
# a. Use case: [7, 9, -3, -32, 107, -1, 36, 95, -14, -99, 21]
def pos_and_neg_num(list_of_nums):
#instantiate variables to hold values
pos_count = 0
neg_sum = 0
#loop through each number check if its less than or more than 0
for num in list_of_nums:
if num < 0:
neg_sum += num
elif num > 0:
pos_count += 1
return [pos_count, neg_sum]
print(pos_and_neg_num([7, 9, -3, -32, 107, -1, 36, 95, -14, -99, 21]))
# 6. Create a method that accepts a string of space separated numbers and
# returns the highest and lowest number as a string
# a. Use case: “3 9 0 1 4 8 10 2” “0 10”
def highest_lowest_num(string):
# make str a list
list_of_nums = string.split(' ')
# iterate through list making each value a num for comparison
for i in range(0, len(list_of_nums)):
list_of_nums[i] = int(list_of_nums[i])
print(list_of_nums)
# use python methods for cal max min values and turn them back to str
max_num = str(max(list_of_nums))
min_num = str(min(list_of_nums))
# return as an f string
return f'{min_num} {max_num}'
print(highest_lowest_num("3 9 0 1 4 8 10 2"))
# 7. Create a method that accepts a string, check if it’s a valid email address and
# returns either true or false depending on the valuation. Think about what is
# necessary to have a valid email address.
# a. Use case:
# i. “mike1@gmail.com” true
# ii. “gmail.com” false
def is_valid_email(str):
# instantiate variables to hold boolean values to check two conditions
has_dot = False
has_at_sym = False
# check each char in str for . or @
for char in str:
if char == '.':
has_dot = True
elif char == '@':
has_at_sym = True
# If both are true, return true, else return false
if has_dot and has_at_sym:
return True
else:
return False
print(is_valid_email('gmail.com'))
# 8. Create a method that takes in a string and replaces each letter with its
# appropriate position in the alphabet and returns the string
# a. Use case:
# i. “abc” “1 2 3”
# ii. “coding is fun” “3 15 4 9 14 7 9 19 6 21 14”
def alpha_to_pos(string):
alpha = '1abcdefghijklmnopqrstuvwxyx'
alpha = list(alpha)
new_str = ''
for char in string:
new_str = new_str + (str(alpha.index(char)) + ' ')
return new_str
print(alpha_to_pos('hello'))
# To be a good problem solver, it is important to be able to break problems down. One way to go about
# this is to write out the steps it will take to solve the problem. These steps are written down in English in
# a manner that are easily explainable to someone who may not be technical. The idea is that in order to
# code something out, you first need to have a good understanding of what it is you are attempting to
# solve.
# For each of the problem solving problems below, write out the steps it will take to go about solving the
# problem. For example, once you are done writing out the steps for the leap years problem, you would
# then write out the code to solve the problem. You would then repeat the process for each ensuing
# problem. Ideally, this will be a good habit that you will develop and carry forward with you for all
# problems you encounter at devCodeCamp and beyond.
# 1. Write a method that prints the next 20 leap years.
# ================MY STEPS===================
# If a year is a leap year, it is divisible by 4. (we can use modules operator)
# Find python method for getting date of now for starting point.
# Store year in variable and increment year 20 times to compar % 4 to.
# If it's a year that % 4 == 0, store that year as an element in a list
# return that list.
# ================MY STEPS===================
from audioop import reverse
from datetime import date
next_twenty_leap_years = []
def next_twenty_leap_years():
current_year = int(date.today().strftime("%Y"))
for i in range(0, 20):
if current_year % 4 == 0:
print('Leap Year ==>', current_year)
current_year += 1
next_twenty_leap_years()
# 2. Write a method that finds the longest palindromic substring of a given string.
# ================MY STEPS===================
# Understand what a palindromic substring is.
# -Input: Given string :"forgeeksskeegfor",
# -Output: "geeksskeeg"
# -Input: Given string :"Geeks",
# -Output: "ee"
# If theres more than one, find the longest.
# Iteration is involved
# Compare two strings, one being the reverse of the original
# return the longest palindromic substring
# ================MY STEPS===================
def longest_palindromic_substring(string, num = 2, longest_found = ''):
position1 = 0
position2 = num
while position2 <= len(string):
sub_str = string[position1:position2]
if sub_str == sub_str[::-1]:
if len(sub_str) >= len(longest_found):
longest_found = sub_str
position1 += 1
position2 += 1
if position1 == 1 and position2 == (len(string)) :
return longest_found
num += 1
return longest_palindromic_substring(string, num, longest_found)
print(longest_palindromic_substring('forgeeksskeegfor'))
# 3. Write a method to convert a given number to hours and minutes.
# ================MY STEPS===================
# Number? Is the number seconds? Is the number days? Let's go with seconds 20520
# How many seconds is in an hour? 3600
# How many seconds is in a minute? 60
# First determine the hours (need a variable)
# -Then determine the minutes with left over (also need a variable)
# Return as an f string
# ================MY STEPS===================
import math
def hours_and_minutes(seconds):
if seconds < 1:
return 'Please input a number that can be evaluated larger than 0!'
hours = 0
minutes = 0
if seconds > 3600:
hours = math.floor(seconds/3600)
seconds = (seconds - hours * 3600)
minutes = math.floor(seconds/60)
seconds = (seconds - minutes * 60)
elif seconds < 3600 and seconds > 60:
minutes = math.floor(seconds/60)
seconds = (seconds - minutes * 60)
return f'The seconds in question are equal to {hours} hours, {minutes} minutes, and {seconds} seconds!'
print(hours_and_minutes(20520))
# 4. Write a method to get the difference between a given number and 13, if the number is
# greater than 13 return double the absolute difference
# ================MY STEPS===================
# Absolute value is the distance between two points
# Think of the bar line with 0 in the middle
# Use python method to return absolute value
# handle if statement for num larger than 13, doubling the value
# return absolute difference
# ================MY STEPS===================
def abs_diff(num):
difference = 13 - num
if num > 13:
difference = difference * 2
return abs(difference)
print(abs_diff(-2))
# 5. Write a method to check from three given numbers (non negative integers) that two or
# all of them have the same rightmost digit.
# ================MY STEPS===================
# Three numbers any size for inputs num1 num2 num3
# Find a way to get last value of each for comparison, (length - 1)?
# Compare all three with if statements to return one of the strings below
# return 'None of these numbers have the same right most digit'
# return 'Two of these numbers have the same right most digit'
# return 'All three of these numbers have the same right most digit'
# ================MY STEPS===================
def compare_last_digit(num1, num2, num3):
num1 = str(num1)
num2 = str(num2)
num3 = str(num3)
num1 = num1[len(num1)-1]
num2 = num2[len(num2)-1]
num3 = num3[len(num3)-1]
if num1 == num2 and num1 == num3:
return 'All three of these numbers have the same right most digit'
elif num1 == num2 or num1 == num3 or num2 == num3:
return 'Two of these numbers have the same right most digit'
else:
return 'None of these numbers have the same right most digit'
print(compare_last_digit(23,31,456))
# 6. Write a method to check if the characters a and b are separated by exactly 3 places
# anywhere (at least once) in a given string.
# ================MY STEPS===================
# Input is a string
# Check if both char a and b are present, if not return
# If they both are present get the distance between (absolute diff?)
# return true if distance is 3, if not return false
# ================MY STEPS===================
def is_dist_of_three_ab(string):
contains_a = string.__contains__('a')
contains_b = string.__contains__('b')
if contains_a and contains_b:
index1 = string.find('b')
index2 = string.find('a')
distance = abs(index1 - index2)
if distance == 3:
return True
else: return False
return False
print('dist between a and b 3? ====>', is_dist_of_three_ab('jollyas bee'))
# 7. Write a method to check if a given string contains equal number of p's and t's present.
# ================MY STEPS===================
# Two variables to hold a count of p's and t's in a string
# Iterate and update count for each count var
# Compare the var to see if equal
# If equal return true, else return false
# ================MY STEPS===================
def is_p_and_t_equal_count(string):
string = string.lower()
count_p = 0
count_t = 0
for char in string:
if char in string == 'p':
count_p += 1
elif char == 't':
count_t += 1
if count_p == count_t:
return True
else: return False
print('p and t count same? ====>',is_p_and_t_equal_count('hgsd nbvs thngtt nbfppk p'))
# 8. Write a method to compute the sum of all digits that occur in a given string.
# ================MY STEPS===================
# est a var to hold sum (result)
# make nums a string for iteration
# on each iteration flip str back to num and add it to variable result
# return the variable result
# ================MY STEPS===================
def add_all_digits(num):
result = 0
num = str(num)
for digit in num:
result += int(digit)
return result
print(add_all_digits(1232))
# 9. Write a method to check whether a given fraction is proper or not.
# Note: There are two types of common fractions, proper or improper. When the
# numerator and the denominator are both positive, the fraction is called proper if the
# numerator is less than the denominator, and improper otherwise.
# ================MY STEPS===================
#
# ================MY STEPS===================
# 10. Write function that translates a text to Pig Latin and back. English is translated to Pig
# Latin by taking the first letter of every word, moving it to the end of the word and
# adding ‘ay’. “The quick brown fox” becomes “Hetay uickqay rownbay oxfay”.
# Problem Solving With Lists
# 11. Write a method that returns the largest element in a list.
# 12. Write a method that rotates a list by k elements. You input a value for k that represents
# how many places in the listyou want to rotate by. For example, if k is rotated by 2,
# [1,2,3,4,5,6] becomes [3,4,5,6,1,2]. Try solving this without creating a copy of the list.
# How many swap or move operations do you need?
# 13. Write methods that add, subtract, and multiply two numbers in their digit-
# listrepresentation (and return a new digit list). If you’re ambitious you can implement
# Karatsuba multiplication. Try different bases. What is the best base if you care about
# speed?
```