Skip to content

Instantly share code, notes, and snippets.

View swift-student's full-sized avatar

Shawn Gee swift-student

View GitHub Profile
import Foundation
struct Post: Decodable {
let id: String
let title: String
let body: String
}
struct GraphQLResult<T: Decodable>: Decodable {
let object: T?

To start with, I will need to figure out how to get a binary representation of an integer. Then I will simply loop through the numbers higher and lower until I find an integer that has the same number of 1's or I hit Int.max or Int.min. I will then return these numbers in a tuple.

Once I had completed my function, I realized that I should have been using UInt instead of Int as the instructions said it should take in a positive integer, so I switched all my code to be working with UInts. I ended up making a couple extensions, that made finding the nubmer of "1"s in the binary representation much shorter to write. Then it was just a matter of creating two for loops, one going up, and one going down to find the next highest and lowest integers with the same number of "1"s in their binary string.

This is my result:

func sameNumberOfBinaryOnes(_ number: UInt) -> (higher: UInt?, lower: UInt?) {
    let numBinaryOnes = number.numBinaryOnes
    

Since we are going to be shuffling the array in place, I will be using an inout parameter. I am not sure I would understand how to do a truly uniform shuffle, as my math skills are not stellar. Instead I will iterate through the array and select another element at random to replace each element. If I have time, I will then attempt to implement arc4random_uniform() instead of using random.

var arrayToShuffle = [6,7,8,9]

func randomShuffle<T>(_ array: inout Array<T>) {
    for index in 0...array.count - 1 {
        let swapIndex = Int.random(in: 0...array.count - 1)
        array.swapAt(index, swapIndex)
 }

To start, the challenge specifies I need to make an extension on string, so I will set that up. Then, since we are ignoring case, I will go ahead and create lowercased versions of both the string passed in and self. I will then see about using regular expressions to determine if self contains the string passed in.

Ok, so turns out, the .rangeOf function has an options parameter which could allow us to use a regular expression, or simply specify .caseInsensitive. So this challenge is solved in one line. Kinda lame. Maybe come up with a harder challenge that forces us to use a regular expression...

extension String {
    func anotherContains(_ string: String) -> Bool {
        self.range(of: string, options: .caseInsensitive) != nil
    }
}

My first idea to epand an integer would be to use the modulo operator starting with 10 and increasing by a power of 10 each time. So if the input was 987 I would do 987 % 10 and get 980. Then I would add 7 to my array, and subtract 7 from my input integer as well. The next time through the loop I would do 980 % 100 and get 80, which should be added to the beginning of my array, since the arrays should be ordered from largest component to smallest. This would repeat one more time with 900 % 1000 which would give me 900. Once I add 900 to my array and subtract it from my integer, my integer would be at 0. This will be my exit condition for my loop.

One thing to consider would be negative input numbers as well as 0. I will have to figure out to handle these conditions after I get it working with positive integers.

After testing my code, it occured to me that by using a repeat while loop, I could get my function to return an array of [0] by letting it execute the loop once with an input value of 0 before eva

So to begin with, to test for a palindrome, I would start at the first and last character and work my way towards the middle comparing the characters. Once I reach halfway, if all the characters were the same, I know that the string is a palindrome.

However, I believe that the question is asking if any permutation of the string (rearranging the string in any way) can form a palindrome. I could rearrange the string into all possible permutations using recursion, however that would be more complicated than simply answering the question "can I make a palindrome from these characters".

To form a palindrome, all characters with the exception of one (the pivot at the middle in the case of an odd number of characters) must have an equal partner. So if I find more than one character that doesn't have an equal partner, my function would return false. Otherwise I can return true.

Also, one must consider the case of, well... case. If the characters are a mix of upper and lower case, should the function factor in ca

Since I am writing a function that takes two positive integers, it would be wise to guard against negative values. After that, I think I would iterate through all possible cominations of numbers that would add up to the sum, and try multiplying them to see if they equal the product. I should divide the sum by two so that I start at the smallest integers before working my way outward, as the instructions are asking for the two smallest positive integers. I would then return an array with the two numbers, with the smaller one preceeding the larger one. This may likely be a very naive solution and may be quite inneficient with some input cases, but it should work.

func sumAndProduct(_ sum: Int, _ product: Int) -> [Int] {
    // First off, let's guard against non-positive integers
    guard sum > 0, product > 0 else { return [] }
    
    // If the sum is odd, dividing by 2 will result in x rounding down
    var x = sum / 2
    // So then we will take the sum and subtract x to get y