SQL Saturday

This is a short post but the problems were still pretty fun!

Types of Triangles:

Given the table of triangles, sides find if a triangle is possible. If so then find the type of triangle that exists.

Triangle


The PADS

An OCCUPATIONS table that contains the following records:

Generate the following two result sets:

  1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
  2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

SQL Saturday

SQL Saturday

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY

FROM STATION

WHERE City like “A%” or City like “E%” or City like “I%” or City like “O%” or City like”U%”;

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u";

Query the list of CITY names from STATION which has vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City like "A%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or
      City like "E%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like "I%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like "O%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like"U%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u");

Query the list of CITY names from STATION that does not start with vowels. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City NOT like "A%" AND City NOT like "E%" AND City NOT like "I%" AND City NOT like "O%" AND City NOT like"U%";

Query the list of CITY names from STATION that does not end with vowels. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City NOT like "%a" AND City NOT like "%e" AND City NOT like "%i" AND City NOT like "%o" AND City NOT like"%u";

Fun with Select Queries

Query all columns for all American cities in CITY with populations larger than 100000. The CountryCode for America is USA. Input Format The CITY table is described as follows:

Screen Shot 2018-01-27 at 9.53.29 AM.png

SELECT * /*Selects all columns from table*/
FROM CITY /*selects table named city */
 /*only if the population is over100000 and the country is in the USA*/
WHERE POPULATION > 100000 AND COUNTRYCODE = 'USA';

Query the names of all American cities in CITY with populations larger than 120000. The CountryCode for America is USA.

SELECT NAME /*Selects NAME column from table*/
FROM CITY /*selects table named city */
/*only if the population is over 120000 and the country is in the USA*/
WHERE POPULATION > 120000 AND COUNTRYCODE = 'USA';

Query all columns (attributes) for every row in the CITY table.

SELECT * /*Selects all columns from table*/
FROM CITY; /*selects table named city */

Query all columns for a city in CITY with the ID 1661.

SELECT * /*Selects all columns from table*/
From CITY /*selects table named city */
WHERE ID = 1661; /*only if the city has an id of 1661 */

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

SELECT * /*Selects all columns from table*/
FROM CITY/*selects table named city */
WHERE COUNTRYCODE = 'JPN'; /*only if the country is in the japan*/

Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

SELECT NAME /*Selects NAME column from table*/
FROM CITY/*selects table named city */
WHERE COUNTRYCODE = 'JPN'; /*only if the country is Japan*/

Query a list of CITY and STATE from the STATION table.

Screen Shot 2018-01-27 at 10.12.18 AM.png
SELECT CITY, STATE /*Selects the city and state columns from table*/
FROM STATION; /*reads from table*/

Query a list of CITY names from STATION with even ID numbers only. You may print the results in any order, but must exclude duplicates from your answer.
SELECT DISTINCT CITY /*selects distinct city name from a table */
FROM STATION /*uses table called station*/
WHERE (ID%2) = 0; /*only if the id is even*/

Let  be the number of CITY entries in STATION, and let  be the number of distinct CITY names in STATION; query the value of  from STATION. In other words, find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

/*selects city count and distinct city name from a table and finds diff*/
SELECT COUNT(CITY) - COUNT( DISTINCT CITY) FROM STATION;

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

/*selects city name and string len from a table */
SELECT CITY, LENGTH(CITY)
/*selects from table named station */
FROM STATION
/*if the string len is the minimum */
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION )
/*orders in alpha by city name */
ORDER BY CITY
/*takes only one from the list*/
LIMIT 1;

/*selects city name and string len from a table */
SELECT CITY, LENGTH(CITY)
/*selects from table named station */
FROM STATION 
/*if the string len is the maximum */
WHERE LENGTH(CITY) = (SELECT MAX(LENGTH(CITY)) FROM STATION)
/*orders in alpha by city name */
ORDER BY CITY
/*takes only one from the list*/
LIMIT 1;

 

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.