Skip to content

Instantly share code, notes, and snippets.

@tangjeff0
Created September 2, 2018 13:58
Show Gist options
  • Save tangjeff0/2b95245bf8247e4676361629eec4d5c1 to your computer and use it in GitHub Desktop.
Save tangjeff0/2b95245bf8247e4676361629eec4d5c1 to your computer and use it in GitHub Desktop.
fn that takes in a number n, and prints a right aligned pyramid such that line 1 has n-1 spaces and 1 hash, line 2 has n-2 spaces and 2 hashes...
// n = 6
// 5 ' ' 1 '#' = 6
// 4 ' ' 2 '#' = 6
// 3 ' ' 3 '#' = 6
// 2 ' ' 4 '#' = 6
// <= inclusive
// < exclusive
/*
* worst case, Big O of O(n*n)
* as n approaches _infinity_, how does the time increase?
* loop 1 - O(n)
* loop 2 - O(n^2)
* total is O(n + n^2) ~ O(n^2)
*/
function staircase(n) {
let stairs = ""; // 1
// 301 O(n)
for (let i = 0; i < n; i++) { // 1, 100, 100 = 201
stairs += " "; // this is not actually bad - 100 iterations
}
// O(n^2)
for (let i = 0; i < n; i++) { // 1, 100, 100
// stairs has len 10, .substring(1) ~> 9 ops
// stairs has len 10, .substring(8) ~> 2 ops
// what is the cost of .substring(param) _IN GENERAL_ - we do not know what we are passing into substring aka what is the Big O for a substring that is n characters long and you use .substring()
// input: n, output: O(n)
// it doesnt matter what how big the input to .substring() is...
// we only care about the worst case
// and the worst case is when the param is small
stairs = stairs.substring(1) + "#" // this is bad
// if stairs = "abc" // 0 1 2
// stairs.substring would equal "bc" // 1 2 -> 0 1
// if stairs were "sadkjfalsifasif" and u juts removed index 0, then each index has to decrement
console.log(stairs) // 100
}
}
/* function sortArr(arr) {
* ...
* }
* what if i told u that if the arr was already sorted, sortArr would
* return immediately
* but that if it was completely really badly out of order, sortArr would
* return after n*n time
* Big O - n*n
* */
/* Nested for loop
*/
function main(n) {
for (var i = 0; i < n; i++) {
var str = ''
for (var j = n-i-1; j > 0; j--) {
str += '-'
}
for (var j = 1; j < i+2; j++) {
str += '#'
}
console.log(str)
}
}
/* 1 - clever/hacky - doesnt tell us much about understanding
* 2 - short, readability is tough, a lot going on
* - captures the intuition v neatly that each row is slightly different, and in each row we increment hashes and decre spaces
* 3 - shows a lot of understanding of control and logic
* - anyone could understand this
* - modular and verbose
*
*
* 3, 1, 2
* 2 - ur intuition to throw shade at nested for loop is right
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
console.log("I run n*n times", i, j) 0-0 0-1 0-2 0-3... 0-n | 1-0 1-1 1-2... 1-n | n-0 n-1 n-2... n-n ~> n*n
}
}
be more nuanced than that
if integers are independent its bad
if you know the inner is constant, not so bad
for ... {
for ... {
}
}
bc we are doing for loop outside before we even make a row, it is bad
def Big O notation: worst-case scenario
1 - time (we usually more care about this)
2 - space
as n gets really big (1, 10, 100, 1000...) how do the function's time and space grow accordingly?
for ...
for ...
O(c) ~ O(1) for some constant c
var num = 1
num++
str += '#'
O(n) - for loop
O(n*n) = O(n^2) - nested for loop, independent iterators
O(n^3)
O(n^4)...
O(log(n))
O(nlog(n^2))...
O(2^n)
* 3 2 1
* 3 - runs n * n
* 2 - also n * n
* 1 -
* */
function main(n) {
var spaces = n - 1
var i = 0
var str = ''
var count = 0
while (i <= n) {
if (i < spaces) {
str += ' '
} else {
str += '#'
}
i++
count++
if (count == n*n) {
break
}
if (i == n) {
str += '\n' // how do we get this loop to not exit afterwards
spaces -= 1
i = 0
}
}
console.log(str)
}
/* while loop
* we know that num spaces + num hashes = n
* based on this knowledge we can print the current string
* whenever it is n long, and then building the next one
* the very first time, we will have n-1 spaces, and 1 #
*
* FOR THE FIRST ROW
* spaces = n-1 // 5 spaces
* i = 0 // the number of chars added so far
* while i is less than n // for a row
* if i <= spaces
* str += ' '
* else
* str += '#'
* i++
* if i == n
* console.log(str)
*
*/
// how do we maintain this while loop
// to continue building the string?
// without using another loop or recursion
//
/* i = 0, spaces = 5, hash = 1
/* i = 0, spaces = 4, hash = 1
*/
function main(n) {
var spaces = n - 1
var i = 0
var str = ''
var rows = 1
var count = 0
while (i <= n) {
if (i < spaces) {
str += ' '
} else {
str += '#'
}
i++
count++
// only breaks out of curr loop
// if (count == n*n){
// break
// }
if (i == n) {
// avoid nested controls when u can
// spaces and rows check the same thing ~> last row but not necessarliy last col
// if (spaces == 0) {
// break
// }
// if (rows == n) {
// break
// }
str += '\n' // how do we get this loop to not exit afterwards
spaces -= 1
i = 0
rows++
}
}
console.log(str)
}
main(6)
@Webprotekh
Copy link

The second solution is the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment