Skip to content

Commit

Permalink
wednesday tests 10/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Giordo committed Feb 10, 2016
1 parent 93d64ef commit 19ff811
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Js/find_sparse_arrays.js
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;
}
}
}
}
*/
13 changes: 13 additions & 0 deletions Js/regex_count_lowercase2.js
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!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~");
8 changes: 8 additions & 0 deletions Py/automate_boring_exercises/exit_example.py
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
24 changes: 24 additions & 0 deletions Py/automate_boring_exercises/first_exercise.py
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.')
"""
25 changes: 25 additions & 0 deletions Py/automate_boring_exercises/flow control.py
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."
"""
7 changes: 7 additions & 0 deletions Py/automate_boring_exercises/for_while_exercise.py
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
9 changes: 9 additions & 0 deletions Py/automate_boring_exercises/hello_func.py
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()
8 changes: 8 additions & 0 deletions Py/automate_boring_exercises/if_exercise.py
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"
3 changes: 3 additions & 0 deletions Py/automate_boring_exercises/printrandom.py
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)

0 comments on commit 19ff811

Please sign in to comment.