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
def get_page(): | |
import urllib2 | |
f = urlib2.urlopen("https://www.google.com") | |
content = f.read() | |
return content | |
content = get_page() |
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
try { // starts a server socket on port 4444 | |
serverSocket = new ServerSocket(4444); // socket listens on 4444 | |
} | |
catch(IOException e) { | |
System.err.println("Could not listen on port : 4444"); | |
System.exit(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
int[] count = new int[cast.length]; // cast array is votes casted | |
int count1, num; | |
for(int i=0; i<cast.length; i++) { | |
num = ++count[cast[i]]; // increments count on vote cast in array | |
if(num > cast.length/2) { | |
System.out.println("Winner is candidate " + cast[i]); // returns the index of winner | |
break; | |
} | |
} |
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
int count = 1, majorityIndex = 0; | |
for(int i=1; i<cast.length; i++) { | |
if(cast[i]==cast[majorityIndex]) | |
count++; | |
else | |
count--; | |
if(count==0) { // no majority at this point | |
majorityIndex=i; | |
count=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
package main | |
import "fmt" | |
func main() { | |
fmt.Println("this is a simple go file") | |
} |
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
console.clear(); | |
var source = Rx.Observable.create(observer => { | |
Rx.Observable.range(0,5).subscribe( | |
data => { | |
console.log(data) | |
if(data > 2) { | |
// conveys to the subscriber of source | |
// that values has changed | |
observer.next("greater than 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
var obs = Rx.Observable.range(0, 10); | |
obs.subscribe( | |
data => { | |
console.log(data); | |
} | |
); |
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
var source = Rx.Observable.create((observer) => { | |
setTimeout(() => { | |
console.log('timeout hit'); | |
observer.onNext('Observable 101'); | |
}, 1000); | |
console.log('observable initialized'); | |
}); | |
source.subscribe(x => console.log(x)); |
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
//Create an observable that emits a value every second | |
const myInterval = Rx.Observable.interval(1000); | |
//Create an observable that emits every time button is clicked | |
var button = document.getElementById('clickButton'); | |
const bufferBy = Rx.Observable.fromEvent(button, 'click'); | |
/* | |
Collect all values emitted by our interval observable until we click document. This will cause the bufferBy Observable to emit a value, satisfying the buffer. Pass us all collected values since last buffer as an array. |
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
//emit (1,2,3,4,5) | |
const source = Rx.Observable.from([1,2,3,4,5]); | |
//add 10 to each value | |
const example = source.map(val => val + 10); | |
//output: 11,12,13,14,15 | |
const subscribe = example.subscribe(val => console.log(val)); | |
//emit ({name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50}) | |
const sourceTwo = Rx.Observable.from([{name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50}]); | |
//grab each persons name |
OlderNewer