Skip to content

Instantly share code, notes, and snippets.

@stahnni
stahnni / A maximal subarray
Created May 15, 2017 20:13
A maximal subarray
/*
The input is an array of numbers, e.g. arr = [1, -2, 3, 4, -9, 6].
The task is: find the contiguous subarray of arr with the maximal sum of items.
Write the function getMaxSubSum(arr) that will find return that sum.
For instance:
getMaxSubSum([-1, 2, 3, -9]) = 5 (the sum of highlighted items)
@stahnni
stahnni / Sum input numbers
Last active May 14, 2017 05:38
Sum input numbers
/*
Write the function sumInput() that:
Asks the user for values using prompt and stores the values in the array.
Finishes asking when the user enters a non-numeric value, an empty string, or presses “Cancel”.
Calculates and returns the sum of array items.
P.S. A zero 0 is a valid number, please don’t stop the input on zero.
*/
@stahnni
stahnni / FizzBuzz
Last active May 13, 2017 08:30
FizzBuzz
/*Eloquent JS
FizzBuzz
Write a program that uses console.log to print all the numbers from 1
to 100, with two exceptions. For numbers divisible by 3, print "Fizz"
instead of the number, and for numbers divisible by 5 (and not 3), print
"Buzz" instead.
When you have that working, modify your program to print "FizzBuzz",
for numbers that are divisible by both 3 and 5 (and still print "Fizz" or
"Buzz" for numbers divisible by only one of those).
@stahnni
stahnni / chess board
Created May 13, 2017 07:39
Chess board
/*
Chess board
Write a program that creates a string that represents an 8×8 grid, using
newline characters to separate lines. At each position of the grid there
is either a space or a “#” character. The characters should form a chess
board.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
@stahnni
stahnni / looping a riangle
Created May 13, 2017 07:19
looping a triangle
/*
Looping a triangle (eloquent Javascript ex)
Write a loop that makes seven calls to console.log to output the following
triangle:
#
##
###
####
#####
######
@stahnni
stahnni / adjacentElementsProduct
Created May 11, 2017 17:41
adjacentElementsProduct from codefights
/*
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
Example
For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.
7 and 3 produce the largest product.
@stahnni
stahnni / century
Created May 8, 2017 21:36
year from century solution
/*Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.
Example
For year = 1905, the output should be
centuryFromYear(year) = 20;
For year = 1700, the output should be
centuryFromYear(year) = 17.
The solution i tried is below
@stahnni
stahnni / Move image across
Created March 7, 2017 13:00
use tkinter to move an image across screen
"""Try displaying a photo of yourself on the canvas using tkinter.
Make sure it’s a GIF image! Can you make it move across the
screen?"""
import time
from tkinter import *
tk = Tk()
w = 500
h = 500
canvas = Canvas(tk, width=w, height=h)
"""
Make a triangle move across the screen to the right,
then down, then back to the left, and then back to its starting
position. Using tkinter
"""
import time
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
"""
Create a program that asks the user to enter
their name and their age. Print out a message addressed to them that tells them the year
that they will turn 100 years old.
Extras:
Add on to the previous program by asking the user for another number and printing out
that many copies of the previous message. (Hint: order of operations exists in Python)
Print out that many copies of the previous message on separate lines. (Hint: the string