Created
July 10, 2017 09:40
-
-
Save JuneBuug/ea5650104be8f58a8affc96d48fd7d5a to your computer and use it in GitHub Desktop.
PascalTriangle
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
// | |
// main.swift | |
// swift-sample | |
// | |
// Created by woowabrothers on 2017. 7. 10.. | |
// Copyright © 2017년 woowabrothers. All rights reserved. | |
// | |
import Foundation | |
func makePascalTriangle(numofRows : Int) -> [[Int]]{ | |
var result = [[Int]]() | |
for i in 1...numofRows{ | |
var rowArray = [Int]() | |
for j in 1...i{ | |
rowArray.append(recurPascal(row: i, column: j)) | |
} | |
result.append(rowArray) | |
} | |
return result | |
} | |
func recurPascal(row: Int ,column :Int) -> Int { | |
if(column == 1){ | |
return 1 ; | |
}else if( row == column){ | |
return 1 ; | |
}else{ | |
return recurPascal(row: row - 1 , column : column - 1) + recurPascal(row: row - 1 , column: column) | |
} | |
} | |
for argument in CommandLine.arguments { | |
switch argument { | |
default: | |
let arg = Int(argument) | |
if( arg != nil ){ | |
print(makePascalTriangle(numofRows: arg!)) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment