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
""" | |
Go to the following link and paste the below code: https://www.hackerrank.com/challenges/ctci-balanced-brackets/problem | |
Issue: An empty deque (or list) "magically" has uninserted values present when initialized. | |
Steps to reproduce: | |
1) Run this code to see it passes the test cases. | |
2) Comment out the __init__() method in the stack, and replace "contents = None" with "contents = deque()" or "contents = []" | |
3) Run to observe it fails the test cases. | |
4) Beneath "s = stack()" in is_matched(), create a new line and put "s.dump()" on it. |
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
#!/usr//bin/python | |
from collections import * | |
""" | |
Question: "Write a function that left-shifts (or right-shifts) an array arr by n places." | |
Answer: Use a deque. Note that you can do this too with normal arrays, but when you get | |
asked about performance, deques have O(1) performance for popping or appending from | |
either side, and are as easy to use as normal arrays. |
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
#!/bin/python | |
# Problem statement: https://www.hackerrank.com/challenges/reduced-string/problem | |
import sys | |
def remove_first_duplicates(s): | |
for i in range(0, len(s)-1): | |
if s[i] == s[i+1]: | |
return s[:i] + s[i+2:], True | |
return s, False |
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
people = [] | |
more = 'y' | |
while (more == 'y'): | |
name = raw_input("What is your name? ") | |
age = raw_input("What is your age? ") | |
people.append([name, age]) | |
more = raw_input("Add another person? [y/n] ") | |
for each in people: | |
print each[0]+",", "age", each[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
#include <stdio.h> | |
int main(){ | |
int i; | |
int nums[5] = {0,1,2,3,4}; | |
printf("Array is {0,1,2,3,4}, so the first char is arr[0] and the last one is arr[4], but len(arr) is 5.\n\n"); | |
printf("Generally, your options for going through arrays are: \n\n"); | |
printf("for (i = 0; i < len; i++)\n"); |
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
package main | |
import "fmt" | |
func main() { | |
mychan := make(chan int) | |
mychan <- 666 | |
go func() { | |
mychan <- 1 | |
mychan <- 2 |
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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
var numCalcsCreated int | |
calcPool := &sync.Pool{ |
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
""" | |
Examples of spiral-form printing: | |
Input: | |
1 2 3 4 | |
5 6 7 8 | |
9 10 11 12 | |
13 14 15 16 | |
Output: | |
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 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
""" | |
An n-free binary list is a list of binary strings in which there is no string | |
contains n consecutive 1s or 0s. | |
Write a function that takes integers n and m as input, and returns all n-free | |
strings of length m (assume m can be any size but for testing doesn't need to | |
be larger than 10). | |
The solution here generates all possible binary strings, then removes any that | |
have n consecutive 1s or 0s. |
NewerOlder