-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alessandro Giordo
committed
Feb 10, 2016
1 parent
93d64ef
commit 19ff811
Showing
9 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
var dense = []; // declaring global variable to be picked up by function below | ||
Array.prototype.isSparse = function() { | ||
dense = this.filter(function() { // filters the sparse array looking for undefined elements | ||
return true; // to remove and returns the valid elements to the dense array | ||
}); | ||
if (dense.length < this.length) // if it's sparse the dense array will be x = sparse element short | ||
return true; // [,,,,,,] > [] or [,1,2,3] > [1,2,3] | ||
else return false; // if length is same array is not sparse. | ||
}; | ||
|
||
// shorter | ||
|
||
Array.prototype.isSparse = function() { | ||
return this.length !== Object.keys(this).length; // | ||
} | ||
|
||
// test it below | ||
arr1 = [,,,,,]; | ||
arr2 = [1,2,3,,]; | ||
arr3 = [1,2,3]; | ||
Array.prototype.isSparse = function() { | ||
console.log(Object.keys(this).length); | ||
return this.length !== Object.keys(this).length; // | ||
} | ||
arr1.isSparse(); // returns log 0 and true while arr3 is 3 and false | ||
|
||
|
||
// another option with more test cases: | ||
|
||
/*there is one big problem with your test cases. | ||
Say we have an array as follows : | ||
var arr = [,1,2] | ||
We can use Object.keys to tell if arr is sparse. | ||
Object.keys(arr) // -> 2 != arr.length | ||
We then add a non-numeric property to arr | ||
arr.abc = 55 | ||
Object.keys now return a false positive. | ||
Object.keys(arr) // 3 == arr.length | ||
*/ | ||
|
||
Array.prototype.isSparse = function() { | ||
for(var i = 0; i < this.length; i++){ | ||
if(!(i in this)) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/* | ||
for (var keys in this){ | ||
if (Object.keys(this).length === 0) return true; | ||
} | ||
if (this[i] == undefined ) return false; | ||
Array.prototype.isSparse = function() { | ||
for(i = 0; i < this.length; i++){ | ||
if (this[i] == null || this[i] == undefined){ | ||
console.log(this[i] + " is null"); | ||
if (this[i] + this[i+1] == NaN) { | ||
console.log(this[i] + this[i+1] + " is NaN"); | ||
return true; | ||
} | ||
else { | ||
console.log(this[i] + "is not null"); | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function lowercaseCount(str){ | ||
var count = str.match(/[a-z]/g); | ||
if (count === null || count == 0) return 0; | ||
else return count.length; | ||
} | ||
|
||
//better: | ||
|
||
function lowercaseCount(str){ | ||
return (str.match(/[a-z]/g) || []).length | ||
} | ||
lowercaseCount("ABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); | ||
lowercaseCount("abcABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import sys | ||
|
||
while True: | ||
print "Type exit to exit" | ||
response = raw_input() | ||
if response == "exit": | ||
sys.exit() | ||
print "You typed %s." % response |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# this program says hello and asks for my name | ||
|
||
print "Hello World!" | ||
print "What is your name?" # asks for name | ||
my_name = raw_input() | ||
print "It is good to meet you, %s" % my_name | ||
print "The length of your name is %d" % len(my_name) | ||
print "What is your age?" # ask for age | ||
my_age = raw_input() | ||
print "You will be %d in a year" % (int(my_age) + 1) | ||
|
||
# this below works in Python 3 | ||
|
||
""" | ||
print('Hello world!') | ||
print('What is your name?') # ask for their name | ||
myName = input() | ||
print('It is good to meet you, ' + myName) | ||
print('The length of your name is:') | ||
print(len(myName)) | ||
print('What is your age?') # ask for their age | ||
myAge = input() | ||
print('You will be ' + str(int(myAge) + 1) + ' in a year.') | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
# while loop break | ||
while True: | ||
print "Please type your name." | ||
name = raw_input() | ||
if name == "your name": | ||
break | ||
print "Thank you!" | ||
""" | ||
|
||
""" | ||
# while loop continue | ||
while True: | ||
print "Who are you?" | ||
name = raw_input() | ||
if name != "Joe": | ||
continue | ||
print "Hello, %s. What is the password? (it is a fish.)" % name | ||
password = raw_input() | ||
if password == "swordfish": | ||
break | ||
print "Access granted." | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
for number in range(11): | ||
print number | ||
|
||
n = 0 | ||
while n < 11: | ||
print n | ||
n += 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def hello(): | ||
print "Howdy" | ||
print "Howdy!!!" | ||
print "Hello there." | ||
|
||
|
||
hello() | ||
hello() | ||
hello() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
print "How much spam?" | ||
spam = input() | ||
if spam == 1: | ||
print "Hello" | ||
elif spam == 2: # if would also allow printing the else statement here | ||
print "Howdy" | ||
else: | ||
print "Greetings" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import random | ||
for i in range(5): | ||
print random.randint(1, 10) |