Skip to content

Instantly share code, notes, and snippets.

View ankit92ios's full-sized avatar
🎯
Focusing

Ankit ankit92ios

🎯
Focusing
View GitHub Profile
John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
For example, there are socks with colors . There is one pair of color and one of color . There are three odd socks left, one of each color. The number of pairs is .
Function Description
Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.
sockMerchant has the following parameter(s):
@ankit92ios
ankit92ios / Counting Valleys
Created March 30, 2019 10:23
Counting Valleys
This is my solution for hackerrank's Counting Valleys problem in Swift Language.
Problem :
Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step he took, he noted if it was an uphill, , or a downhill, step. Gary's hikes 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 Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.
For example, if Gary's path is , he first enters a valley units deep. Then he climbs out an up onto a mountain units high. Finally, he returns to se
//Electronics Shop
//Problem
/*
Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the items, given her budget.
Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items.
For example, suppose she has to spend. Three types of keyboards cost . Two USB drives cost . She could purchase a , or a . She chooses the latter. She can't buy more than items so she can't spend exactly .
Function Description
/*
Cats and a Mouse
Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse doesn't move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.
You are given queries in the form of , , and representing the respective positions for cats and , and for mouse . Complete the function to return the appropriate answer to each query, which will be printed on a new line.
If cat catches the mouse first, print Cat A.
If cat catches the mouse first, print Cat B.
If both cats reach the mouse at the same time, print Mouse C as the two cats fight and mouse escapes.
For example, cat is at position and cat is at . If mouse is at position , it is units from cat and unit from cat . Cat will catch the mouse.
@ankit92ios
ankit92ios / Compare the Triplets
Created April 2, 2019 12:53
Compare the Triplets
/*
Compare the Triplets
Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from to for three categories: problem clarity, originality, and difficulty.
We define the rating for Alice's challenge to be the triplet , and the rating for Bob's challenge to be the triplet .
Your task is to find their comparison points by comparing with , with , and with .
If , then Alice is awarded point.
If , then Bob is awarded point.
/*
Diagonal Difference
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = . The right to left diagonal = . Their absolute difference is .
/*
10 Days of Statistics
Objective
In this challenge, we practice calculating the mean, median, and mode. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an array, , of integers, calculate and print the respective mean, median, and mode on separate lines. If your array contains more than one modal value, choose the numerically smallest one.
Note: Other than the modal value (which will always be an integer), your answers should be in decimal form, rounded to a scale of decimal place (i.e., , format).
@ankit92ios
ankit92ios / Day 0:weightMean
Created April 3, 2019 08:49
Day 0:weightMean
import Foundation
//Day 0:weightMean
func weightMean(numbers: [Int] , weights : [Int]) -> Double {
var sum = 0.0
var totalWeights = 0
for i in 0..<numbers.count {
sum += Double(numbers[i] * weights[i])
totalWeights += weights[i]
}
let weightMean = sum / Double(totalWeights)
@ankit92ios
ankit92ios / Day 1: Standard Deviation
Created April 3, 2019 08:59
Day 1: Standard Deviation
import Foundation
//Day 1: Standard Deviation
func StandardDeviation(numbers: [Int]) -> Double {
var sum = 0.0
for eachNo in numbers {
sum += Double(eachNo)
}
let mean = sum / Double(numbers.count)
var sumOfSquare : Double = 0
import Foundation
//Day 1: Quartiles
func medianFunc(numbers: [Int]) -> Double {
let sortedNumbers = numbers.sorted(by: { num1, num2 in
return num1 < num2 })
let midIndex = numbers.count / 2
var median = Double(sortedNumbers[midIndex])
if(numbers.count % 2 == 0){
median = Double(sortedNumbers[midIndex] + sortedNumbers[midIndex - 1]) / Double(2)