#sample solution
def simpleSort(arr):
n = len(arr)
for i in range(n):
j = 0
stop = n - i
while j < stop - 1:
if arr[j] > arr[j + 1]:
temp = arr[j+1]
arr[j+1] = arr[j]
arr[j] = temp
j += 1
return arr
Quotes help make search much faster. Example: "Practice Makes Perfect"
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Thursday, December 22, 2016
Codefights def simpleSort(arr)
Codefights def modulus(n)
#sample solution
def modulus(n):
if isinstance( n, int ) :
return n % 2
else:
return -1
def modulus(n):
if isinstance( n, int ) :
return n % 2
else:
return -1
Thursday, April 10, 2014
What hotkeys can you use in a Python shell to repeat the previous command you typed?
Answer:
Alt - p
p stands for previous. If you keep pressing Alt - p, you will continue cycling back through commands you typed before. If you press Alt - n, you will cycle forward. n stands for next.
Alt - p
p stands for previous. If you keep pressing Alt - p, you will continue cycling back through commands you typed before. If you press Alt - n, you will cycle forward. n stands for next.
Monday, October 28, 2013
#Python #Codecademy File Input/Output Case Closed?
# Sample solution
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
if not my_file.closed:
my_file.close()
print my_file.closed
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
if not my_file.closed:
my_file.close()
print my_file.closed
#Python #Codecademy File Input/Output Try It Yourself
# Sample solution
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
#Python #Codecademy File Input/Output PSA: Buffering Data
# Sample solution
# Open the file for reading
read_file = open("text.txt", "r")
# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")
write_file.close()
# Try to read from the file
print read_file.read()
read_file.close()
# Open the file for reading
read_file = open("text.txt", "r")
# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")
write_file.close()
# Try to read from the file
print read_file.read()
read_file.close()
#Python #Codecademy File Input/Output Reading Between the Lines
# note: The instructions state to use "text.txt" but an error comes up stating that the file does not exist
# as a work around, I used "output.txt" instead
my_file = open("output.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
#Python #Codecademy File Input/Output Reading
# Sample solution
my_file = open("output.txt","r")
print my_file.read()
my_file.close()
my_file = open("output.txt","r")
print my_file.read()
my_file.close()
#Python #Codecademy File Input/Output Writing
# Sample solution
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
# Add your code below!
for i in my_list:
my_file.write(str(i)+"\n")
my_file.close()
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
# Add your code below!
for i in my_list:
my_file.write(str(i)+"\n")
my_file.close()
#Python #Codecademy File Input/Output The open() Function
# Sample Solution
my_file = open("output.txt","r+")
my_file = open("output.txt","r+")
Friday, October 11, 2013
#LearnStreet: Teacher's Preview Assignment Lesson 10 Sample Solution
def who_are_you(green, large):
# Write if-elif-else statements below
if green and large:
return "hulk"
elif green:
return "alien"
elif large:
return "elephant"
else:
return "Bruce Banner"
#This is just for you to see what happens when the function is called
print who_are_you(True, True)
# Write if-elif-else statements below
if green and large:
return "hulk"
elif green:
return "alien"
elif large:
return "elephant"
else:
return "Bruce Banner"
#This is just for you to see what happens when the function is called
print who_are_you(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 9 Sample Solution
def kitchen_or_bed(hungry, thirsty):
# your if statement here
if hungry or thirsty:
return "go to kitchen"
else:
return "go to bed"
#This is just for you to see what happens when the function is called
print kitchen_or_bed(True, True)
# your if statement here
if hungry or thirsty:
return "go to kitchen"
else:
return "go to bed"
#This is just for you to see what happens when the function is called
print kitchen_or_bed(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 8 Sample Solution
def should_eat(hungry, awake):
#your if statement here
if hungry and awake:
return "eat"
else:
return "don't eat"
#This is just for you to see what happens when the function is called
print should_eat(True, True)
#your if statement here
if hungry and awake:
return "eat"
else:
return "don't eat"
#This is just for you to see what happens when the function is called
print should_eat(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 7 Sample Solution
def check_interval():
#Your code here
if num >= 1 & num <=5:
return "1-5"
elif num >= 6 & num <=10:
return "6-10"
elif num >= 11 & num <=15:
return "11-15"
elif num >= 16 & num <=20:
return "16-20"
else:
return "not in between 1-20"
#This is just for you to see what happens when the function is called
print check_interval()
#Your code here
if num >= 1 & num <=5:
return "1-5"
elif num >= 6 & num <=10:
return "6-10"
elif num >= 11 & num <=15:
return "11-15"
elif num >= 16 & num <=20:
return "16-20"
else:
return "not in between 1-20"
#This is just for you to see what happens when the function is called
print check_interval()
#LearnStreet: Teacher's Preview Assignment Lesson 6 Sample Solution
def check_length(phrase):
# your if condition here
if len(phrase) < 30:
return 1
# your elif condition here
elif len(phrase) == 30:
return 2
# your else condition here
else:
return 3
#This is just for you to see what happens when the function is called
print check_length("hi, i am a phrase")
# your if condition here
if len(phrase) < 30:
return 1
# your elif condition here
elif len(phrase) == 30:
return 2
# your else condition here
else:
return 3
#This is just for you to see what happens when the function is called
print check_length("hi, i am a phrase")
#LearnStreet: Teacher's Preview Assignment Lesson 5 Sample Solution
def colorful_conditions():
color = "blue"
if color == "red":
return "first block"
elif color == "white":
return "second block"
elif color == "blue":
return "third block"
else:
return "fourth block"
#This is just for you to see what happens when the function is called
print colorful_conditions()
color = "blue"
if color == "red":
return "first block"
elif color == "white":
return "second block"
elif color == "blue":
return "third block"
else:
return "fourth block"
#This is just for you to see what happens when the function is called
print colorful_conditions()
#LearnStreet: Teacher's Preview Assignment Lesson 4 Sample Solution
def check_condition():
#return this if the condition is True
ifRun = "if code block run"
#return this is if the condition is False
elseRun = "else code block run"
if condition == True:
return ifRun
else:
return elseRun
#This is just for you to see what happens when the function is called
print check_condition()
#return this if the condition is True
ifRun = "if code block run"
#return this is if the condition is False
elseRun = "else code block run"
if condition == True:
return ifRun
else:
return elseRun
#This is just for you to see what happens when the function is called
print check_condition()
#LearnStreet: Teacher's Preview Assignment Lesson 3 Sample Solution
flag = "unchanged"
def always_false():
global flag
if False:
flag = "changed"
#This is just for you to see what happens when the function is called
always_false()
print flag
def always_false():
global flag
if False:
flag = "changed"
#This is just for you to see what happens when the function is called
always_false()
print flag
#LearnStreet: Teacher's Preview Assignment Lesson 2 Sample Solution
def always_true():
if 1 == 1:
#insert your code here
return "complete"
#This is just for you to see what happens when the function is called
print always_true()
if 1 == 1:
#insert your code here
return "complete"
#This is just for you to see what happens when the function is called
print always_true()
#LearnStreet: Teacher's Preview Assignment Lesson 1 Sample Solution
def check_wounds():
#your code here
arms = 0
if arms == 1:
return "tis but a scratch"
elif arms == 0:
return "flesh wound"
else:
return "cross bridge"
#This is just for you to see what happens when the function is called
print check_wounds()
#your code here
arms = 0
if arms == 1:
return "tis but a scratch"
elif arms == 0:
return "flesh wound"
else:
return "cross bridge"
#This is just for you to see what happens when the function is called
print check_wounds()
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
//Sample Solution const gameState = {} function preload() { this.load.image('codey', 'https://s3.amazonaws.com/codecademy-...
-
//sample solution function guessNumber(number, clue) { // Prompt the user for a number using the string value of clue guess = promp...
-
//sample solution //Here is the original card object var card1 = {"suit":"clubs", "rank": 8}; //Create a...
-
#sample solution def colorful_conditions(): color = "blue" if color == "red": return "firs...
-
function checkAnswerWrongPlace(ans, realanswer){ // This method compares two input strings representing a player's guess ("an...
-
Instruction: Find the id of the flights whose distance is below average for their carrier. Sample Solution: SELECT id FROM flights AS f...
-
# note: The instructions state to use "text.txt" but an error comes up stating that the file does not exist # as a work around, ...
-
# Sample Solution # Import packages import numpy as np import pandas as pd # Read in transactions data transactions = pd.read_csv(...
-
# Sample Solution from song_data import songs import numpy as np q1 = np.quantile(songs, 0.25) #Create the variables q3 and interquarti...
-
/*sample solution for style.css*/ body > p { background: green; } p { background: yellow; }