This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Problem 4 | |
// "Write a function that given a list of non-negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// This solution first finds the permutations of the integers in the given array. A string is made out of the digits of each permutation, the string is converted to an integer, and the maximum of all these integers is returned. | |
// It is expensive! but it's short and guaranteed to work every time. | |
// I updated objc.io's permutation snippet to Swift 2.0 and used it in my solution | |
// https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Problem 2 | |
// "Write a function that combines two lists by alternatingly taking elements. For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3]." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
// Assumption: homogeneous arrays, same length | |
let first = ["a", "b", "c"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Problem 1 | |
// "Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
let series = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] | |
let realSum = series.reduce(0, combine: +) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Problem 3 | |
// "Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
func fibonacci(count: Int) -> [Double] { | |
var n1: Double = 0 | |
var n2: Double = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Cube digit pairs | |
//: [Problem 90](https://projecteuler.net/problem=90) | |
//: Xcode 7.0, Swift 2.0 | |
/*: | |
Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. | |
For example, the square number 64 could be formed: | |
![Two cubes showing 64](https://projecteuler.net/project/images/p090.gif) | |
In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Coded Triangle Numbers | |
//: [Problem 42](https://projecteuler.net/problem=42) | |
//: Xcode 7.0, Swift 2.0 | |
/*: | |
The nth term of the sequence of triangle numbers is given by, t[n] = ½n(n+1); so the first ten triangle numbers are: | |
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... | |
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t[10]. If the word value is a triangle number then we shall call the word a triangle word. | |
Using [words.txt](https://projecteuler.net/project/resources/p042_words.txt), a 16K text file containing nearly two-thousand common English words, how many are triangle words? | |
*/ | |
//: Note: I found and replaced the double quotes directly in the text file rather than in code since escaping the double quotes in a string kept crashing the playground. I named the file "words.txt" in the "Resources" folder of the playground. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: SICP Exercise 1.12 | |
//: //: Xcode 7.0, Swift 2.0 | |
/* The following pattern of numbers is called Pascal’s triangle. | |
1 | |
1 1 | |
1 2 1 | |
1 3 3 1 | |
1 4 6 4 1 | |
. . . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: SICP Exercise 1.11 | |
//: Xcode 7.0, Swift 2.0 | |
//: A function f is defined by the rule that f(n)=n if n<3 and f(n)=f(n−1)+2f(n−2)+3f(n−3) if n≥3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process. | |
func recursive(n: Int) -> Int { | |
if n < 3 { return n } | |
else { return recursive(n-1) + 2 * recursive(n-2) + 3 * recursive(n-3) } | |
} | |
for n in (0...10) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: [Pentagonal Numbers](https://projecteuler.net/problem=44) | |
/*: | |
Pentagonal numbers are generated by the formula, P[n]=n[3n−1]/2. The first ten pentagonal numbers are: | |
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... | |
It can be seen that P[4] + P[7] = 22 + 70 = 92 = P[8]. However, their difference, 70 − 22 = 48, is not pentagonal. | |
Find the pair of pentagonal numbers, P[j] and P[k], for which their sum and difference are pentagonal and D = |P[k] − P[j]| is minimised; what is the value of D? | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: [Sub-string Divisibility](https://projecteuler.net/problem=43) | |
/*: | |
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. | |
Let d[1] be the 1st digit, d[2] be the 2nd digit, and so on. In this way, we note the following: | |
* d[2]d[3]d[4] = 406 is divisible by 2 | |
* d[3]d[4]d[5] = 063 is divisible by 3 | |
* d[4]d[5]d[6] = 635 is divisible by 5 | |
* d[5]d[6]d[7] = 357 is divisible by 7 |
OlderNewer