Electronics Shop

This Hackerrank problem deals with combinations.

The problem:

Sketch

import sys

def getMoneySpent(keyboards, drives, s):
 combo = -1
 for k in keyboards:
 	for d in drives: 
 		if (k+d) > combo and (k+d) <= s:
 			combo = k+d
 
 return str(combo)
 
s,n,m = input().strip().split(' ')
s,n,m = [int(s),int(n),int(m)]
keyboards = list(map(int, input().strip().split(' ')))
drives = list(map(int, input().strip().split(' ')))
# The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
moneySpent = getMoneySpent(keyboards, drives, s)
print(moneySpent)

This problem is pretty straight forward. I used to for loops to cycle through each list.

Another way to do it might be to sort the list eliminate the clearly overly expensive items that are over s and delete those that are  duplicates to cycle through less elements. This might help for very big lists.

 

The Day of the Programmer

This Hackerrank problem asks you given the year, the 256 date of that year according to the Russian Calendar.

Screen Shot 2017-11-05 at 5.35.44 PM

The Code:

import sys

def solve(year):
    months = [31,28,31,30,31,30,31,31]
    
    if year > 1918:
        if year%400 == 0 or (year%4 == 0 and year%100 != 0):
            months[1] = 29
            tmp = sum(months)
            day = 256-tmp
        else:
            tmp = sum(months)
            day = 256-tmp
    elif year == 1918:
        months[1] = 15
        tmp = sum(months)
        day = 256-tmp
    else:
        if year%4 == 0:
            months[1] = 29
            tmp = sum(months)
            day = 256-tmp
        else:
            tmp = sum(months)
            day = 256-tmp
    

   return (str(day) + '.' + "09" + '.' + str(year))
year = int(input().strip())
result = solve(year)
print(result)

For this problem, I set the number of days in the months from January through August into an array and summed them together. After which I subtracted from 256 and got the day. To get the variations in days I altered the month of February based on the calendar system.

Developer Aside:

I took the easy way out in displaying the month since in the Russian calendar system (regardless of the year) the 256 day lands in September.

 

Kangaroo

This is another easy Hackerrank question, Kangaroo.

This problem takes a high school math problem of will one car ever catch another, but instead of cars we are talking kangaroos

kangaroo

def kangaroo(x1, v1, x2, v2):
 if x1 > x2:
    temp = x1
    x1 = x2
    x2 = temp
    temp = v1
    v1 = v2
    v2 = temp 
 
 if v1 <= v2:
    return "NO"
 
 while(x1 < x2):
    x1+=v1
    x2+=v2
 if(x1 == x2):
   return "YES"
 else:
   return "NO"

#given
x1, v1, x2, v2 = input().strip().split(' ')
x1, v1, x2, v2 = [int(x1), int(v1), int(x2), int(v2)]
result = kangaroo(x1, v1, x2, v2)
print(result)

For this problem I set x2 to always be further than x1 by creating a swap condition. After I check if the kangaroo at x1 has a velocity higher than kangaroo at x2 if it isn’t they will never meet. Finally I cycle through until x1 is no longer less than x2.

Apple and Orange

I have been applying to different internships lately and have been referred to hackerrank through many of them so I decided why not add and talk about my solutions to some of the challenges.

This problem is called Apple and Orange and the whole point is to print the number of apples and oranges that fall from Sam’s house.

Here is the problem:

appleOrange

#!/bin/python3

import sys

#given

s,t = input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = input().strip().split(' ')
m,n = [int(m),int(n)]
apple = [int(apple_temp) for apple_temp in input().strip().split(' ')]
orange = [int(orange_temp) for orange_temp in input().strip().split(' ')]

#my code

count = 0
for ap in apple:
     temp = a+ap
     if temp <= t and temp >=s:
         count = count + 1 
print (count)

count = 0
for ora in orange:
    temp = b+ora
    if temp <= t and temp >=s:
         count = count + 1 
print (count)

This problem is fairly simple I used two for loops since the array size for apple and orange can differ and I had to print the total apple and total orange.

 

Mental Note

Mental Note was a note taking application (also the first UWP)  I built. I wanted to try and understand MSA login and async file storage. It was a fun little side project, there are definitely some bugs and design choices I would do differently now. In the end I walked away understanding the ideas of login and file storage for UWP applications.

The project is in my GitHub: GitHub.com/amsuarez01

Here are some screen shots.

This slideshow requires JavaScript.

Habit Hamster

Habit Hamster is a application that will keep track of habits like a reminder list. Current habit tracking apps do not provide a means for users to visually see progress. I am currently working on this in my Software Engineering class and am loving how awesome it is to make UWP applications.

It is currently on my GitHub: GitHub.com/amsuarez01 under Habit hamster.

Here are some Screen shots of the UWP side of development that I have been in charge of.

This slideshow requires JavaScript.

 

Quicksort

The Quick Sort was developed in the early 1960s, by Tony Hoarde, since it became one of the most common sorts. The algorithm performs an in-place comparison sort it utilizes very little extra memory on sorting the string. It has a worst case of O(n^2) and a best case of O(nlogn).

It is first going to select the pivot as the last number in the list

pivot = 58

Then it assigns lo = -1

For each number from 0 to pivot’s position

If a number in that list is less than the pivot number

Then lo is incremented

and it swaps the index number with the number at the lo position

If the number at the end of the list is less than the number at the lo+1 Position

then swap them

Code: step  example

41      67      34      0       69      24      78      58

swapping: 41 with 41

41      67      34      0       69      24      78      58

41      67      34      0       69      24      78      58

swapping: 67 with 34

41      34      67      0       69      24      78      58

swapping: 67 with 0

41      34      0       67      69      24      78      58

41      34      0       67      69      24      78      58

swapping: 67 with 24

41      34      0       24      69      67      78      58

41      34      0       24      69      67      78      58

swapping: 69 with 58

41      34      0       24      58      67      78      69

the pivot point is: 58

41      34      0       24      58      67      78      69

41      34      0       24      58      67      78      69

swapping: 41 with 0

0       34      41      24      58      67      78      69

swapping: 34 with 24

0       24      41      34      58      67      78      69

the pivot point is: 24

0       24      41      34      58      67      78      69

swapping: 41 with 34

0       24      34      41      58      67      78      69

the pivot point is: 34

swapping: 67 with 67

0       24      34      41      58      67      78      69

0       24      34      41      58      67      78      69

swapping: 78 with 69

0       24      34      41      58      67      69      78

the pivot point is: 69

0       24      34      41      58      67      69      78

 

#include “iostream”
#include “random”
using namespace std;
void quickSort(int list[8],int high,int low);
int partition(int list[8],int high,int low);
int main()
{
 int list[8];
 for(int index =0; index < 8; index++)
 {
    list[index] = rand()%100;
    cout << list[index] << ‘\t’;
 }
 cout << endl;
 quickSort(list,7,0);
 for(int index =0; index < 8; index++)
 {
    cout << list[index] << ‘\t’;
 }
 return 0;
}
void quickSort(int list[8],int high,int low)
{
 int temp;
 if(low < high)
 {
    temp = partition(list, high,low);
    cout << “the pivot point is: “ << list[temp] << endl;
    quickSort(list,temp1,low);
    quickSort(list, high, temp+1);
 }
};
int partition(int list[8],int high,int low)
{
 int pivot = list[high];
 int lo = low1;
 int temp;
 for(int index = low; index < high; index++)
 {
    if(list[index] < pivot)
    {
     lo+=1;
     cout << “swapping: “ << list[lo] << ” with “ << list[index] << endl;
     temp = list[lo];
     list[lo] = list[index];
     list[index] = temp;
    }
    for(int index =0; index < 8; index++)
    {
     cout << list[index] << ‘\t’;
    } cout << endl;
 }
 if(list[high] < list[lo+1])
 {
     cout << “swapping: “ << list[lo+1] << ” with “ << list[high] << endl;
    temp = list[lo+1];
    list[lo+1] = list[high];
    list[high] = temp;
 }
 for(int index =0; index < 8; index++)
 {
    cout << list[index] << ‘\t’;
 } cout << endl;
 return lo+1;
};