Jump to content
Sign in to follow this  
ColdMeekly<3

[Challenge] Weekly Python Challenges Week #1

Which difficulty do you Prefer?  

20 members have voted

  1. 1. Difficulty:

    • Easy
      9
    • Medium
      7
    • Hard
      5


Recommended Posts

Welcome to ColdMeekly<3's Weekly Python Challenges!

Ever had the feeling that you feel that there is nothing else to do with Python? Or do you just prefer someone setting you work instead?

Below you will have a chance to work on code from scratch, and learn Python that way, or you could have a look at how other people had solved the problem, and improve their code.

Below are This Weeks Challenges, please do post your approaches if you had made an attempt, and share your experience with newcomers.
Note: For each and every one of these i recommend to use http://trinket.io/ , Unless i specify otherwise.

Also, These do not necessarily have to be done in python.

Challenge #1 - Easy

 

Title: Lightning Distance Calculator


Difficulty: Beginner
Time Needed: 1/10
Details:Have you ever seen a lightning flash or heard the thunder of lightning and wondered how close you were from the lightning strike? Have you noticed that there often was a delay between the flash of light and the clap of thunder when a lightning occurs?
It is possible to calculate the distance to a lightning strike by counting the seconds between the lightning flash and the sound of thunder.
When lightning strikes, the first thing you see is the flash of light which you can see instantly. This is because light travels at a very high speed (Speed of light = 300,000 km/s). At the same time a clap of thunder is created. However, in the air a sound wave does not travel as fast as light, so it may take a few seconds for this clap of thunder to reach you. This depends on how far you are from the lightning.

Your Challenge

Write a Python program that asks the end-user to enter a number of seconds. The program should output the distance (in meters or kilometres) from the lightning strike.

Note that 1 mile = 1.609 km, you may want to convert this distance in miles.


Challenge #2 - Easy

 

Title: Window Cleaner's Quotes


Difficulty: Beginner
Time Needed: 2/10
Details:A window cleaner uses the following pricing policy to calculate how much to charge for cleaning all the windows of his customer’s dwelling. This pricing policy is based on the number of windows that need to be cleaned and works as follows:

  • All quoted prices include a fixed call out fee of £10
  • Then, the first five windows are charged at £2 each
  • The next five windows are charged at £1.50 each
  • Any additional windows are charged at £1 each

Your task is to write a computer program that prompts the end-user to enter the number of windows of their dwelling. The program will then calculate the quoted price using the pricing policy described above and display it to the end-user.


Challenge #3 - Medium

 

Title: Sweet Shop


Difficulty: Intermediate
Time Needed: 4/10
Details:Have you ever been in a sweet shop to buy sweets? For this challenge we are going to spend £5 in a sweet shop hence we need to find out how many sweets we can afford. We will want to pick and mix sweets until we have spent all our money.

To help us buy our sweets we are going to write a program that will help us decide how many sweets we can afford while allowing us to pick and mix different types of sweets.

Here are the sweets available in the shop:
          ---Price List---
A - Marshmallow: £0.20
B - Bubble Gum: £0.30
C - Jelly Bean: £0.15
D - Candy Stick: £0.35
E - Cola Whips: £0.22
X - Exit

Here are the main steps of our program:

  • Display a price list of all the sweets available in the shop,
  • Ask the end-user how much they would like to spend,
  • Ask the user which sweet they would like to buy and how many of them they would like (A to E),
  • Allow the user to enter X (instead of the A to E letter for a sweet) to stop buying more sweets,
  • Check whether the use can afford these and if they can, calculate and display how much money they have left,
  • Repeat steps 3 to 5 for as long as the user has some money left.


#Sweet Shop Challenge


	print(" --- Price List --- ")
	print("")
	print("A -  Marshmallow: $0.20")
	print("B -  Bubble gum: $0.30")
	print("C -  Jelly Bean: $0.15")
	print("D -  Candy Stick: $0.35")
	print("E -  Cola Whips: $0.22")
	print("")
	print("X - Exit")
	print("")
	money = float(input("How much do you want to spend?"))
	#Complete the code here

 


Challenge #4 - Medium

 

Title: Gradient Animation


Difficulty: Intermediate
Time Needed: 6/10
Details:In this challenge we are going to create some animated gradients by progressively changing the color of the screen from one color (e.g. red) to another (e.g. yellow).

RGB Color codes
Did you know that every colour on the screen can be represented using an RGB code (Red, Green, Blue) code. This code consists of three numbers between 0 and 255, indicating how much red, green and blue are used to recreate the colour.

For instance the RGB code for:

    Red is (255,0,0)
    Green is (0,255,0)
    Blue is (0,0,255)
    Yellow is (255,255,0)
    Orange is (255,165,0)

Learning Objective
By completing this code we will investigate how for loops work to iterate through the code a fixed number of times.

We will use an increment in our loop that will go up (increment) by 1 after each iteration, to count from 0 to 255. We will see that we can also use a negative step to count down from 255 to 0.
Our first animation will be based on the following gradient:
50e86a496ff7b72733df2a33a1f7515b.jpg
It will consist of:

    256 frames to shade from red (255,0,0) to yellow (255,255,0)
    + another 256 frames to shade from yellow (255,255,0) back to red (255,0,0)
    It will repeat the above steps 3 times so in total will consist of (256 + 256) x 3 = 1536 frames!

#Gradient Animation
import turtle
import time
window = turtle.Screen()


	#Repeat the pattern 3 times
	for count in range(0,3):
	  #From red (255,0,0) to yellow (255,255,0)
	  for i in range(0,255):
	    red = 255
	    green = i
	    blue = 0
	    window.bgcolor(red,green,blue)
	    time.sleep(0.001)
	    
	  #From yellow (255,255,0) to red (255,0,0)
	  for i in range(255,0,-1):
	    red = 255
	    green = i
	    blue = 0
	    window.bgcolor(red,green,blue)
	    time.sleep(0.001)


Gradient #2: From red (255,0,0) to blue (0,0,255) to red (255,0,0)
69eac80d9cbd71169ae761aaa63cf06d.jpg
This code is slightly different as we want the red code to decrement from 255 to 0 while the blue code increments from 0 to 255. Check how it’s done in the code below:

#Gradient Animation


	import turtle
	import time
	window = turtle.Screen()
	#Repeat the pattern 3 times
	for count in range(0,3):
	  #From red (255,0,0) to blue (0,0,255)
	  for i in range(0,255):
	    red = 255 - i
	    green = 0
	    blue = i
	    window.bgcolor(red,green,blue)
	    time.sleep(0.001)
	    
	  #From blue (0,0,255) to red (255,0,0)
	  for i in range(0,255):
	    red = i
	    green = 0
	    blue = 255 - i
	    window.bgcolor(red,green,blue)
	    time.sleep(0.001)    


Complete the rest :)

Gradient #3: From cyan (0,255,255) to magenta (255,0,255) to cyan (0,255,255)
bcc80bf95d20fbb865136be379de6309.jpg
Gradient #4: From cyan (0,255,255) to yellow (255,255,0) to cyan (0,255,255)
a9ff7920edc9c66fed9ccc819d6709d0.jpg
Gradient #5: From green (0,255,0) to yellow (255,255,0) to magenta (255,0,255) to cyan (0,255,255) to green (0,255,0)
1a9f444ba97a192c328de6b62e391095.jpg
Gradient #6: From black (0,0,0) to white (255,255,255) to black (0,0,0)
d2758c33fc582d314257de4171182055.jpg


Challenge #5 - Hard

 

Title: Molecular Mass Calculator


Difficulty: Advanced
Time Needed: 9/10
Details: Learning Objectives
In this challenge you will improve your string manipulation techniques as well as use Python to perform some basic mathematical calculations.
Did you know?
The molecular weight (mass) may be calculated from the molecular formula of the substance; it is the sum of the atomic weights of the atoms making up the molecule. The molecular mass is expressed in atomic mass unit (amu).

For example, water has the molecular formula H2O, indicating that there are two atoms of hydrogen and one atom of oxygen in a molecule of water.
9b619cf9db9b9452763bc6ebd6975925.PNG
n the following script we use a Python dictionary called atomicMass to store the atomic mass of some of the main atoms (C, H, O…)

We then work out the molecular mass of water: H20.

#Molecular Mass Calculator


	atomicMass = {"H" : 1.008, "O" : 15.999, "C" : 12.011, "N" : 14.007}
	
	molecule = "H2O"
	mass = atomicMass["H"] * 2 + atomicMass["O"]
	print("\nMolecule: " + molecule + " - Mass: " + str(mass))


Your Challenge
Adapt this script to get the program ask the end-user to enter a molecular formula of their choice.
Using string manipulation techniques your Python script will break down the formula entered by the end-user and calculate the total mass of the molecule.

Test your script with the following molecules:
03299479a6dbc14f3f5fe34c6af2410f.PNG

If you are stuck, and require help: Don't hesitate and PM or visit me on TS. Or if liked my challenges and want something else a bit harder, contact me :)

Next week's is coming out on: 27/05/2016

Answers will be released at end of week (My personal take on it)

Edited by ColdMeekly<3

Share this post


Link to post

Nice idea! I've never used Python but I might do some on C just for fun. If I do I'll edit this post with the codes :')

 

Challenge #1 - Easy (in C)

 

 


#include<.....>

int main(){

int secs;
double distmiles, distkm;

printf("\nEnter the number of seconds between the flash of light and the lightnings sound\n");
scanf("%d", &secs);
distmiles = secs/5;
distkm = distmiles*1.609;
printf("\n You are %lf km away from the lightning strike\n",&distkm);

/*or
  
int spsound=340;
double distm;
  
distm=spsound*secs;
distkm=distm/(10^3);
printf("\n You are %lf km away from the lightning strike\n",&distkm);
  
That's a better and faster way of doing it lel*/

return 0;

}

/*I completely forgot how to calculate this so I googled it and apparently you divide the number of seconds by 5 to get the distance (in miles) between you and the lightning. Then I just converted it to km like it was asked by multipling the value by 1.609. I don't know if that's the correct way to calculate it but the code is right I guess.*/

 

 

I just woke up so I probably managed to fuck it up somehow but that's too simple, I'll try a medium one later :p

Share this post


Link to post

Challenge #2 in Java:

int price = 10;
int num = JOptionPane.showInputDialogue("Please enter number of windows requiring cleaning: ");

for (int i=1; i=<num; i++){
  if(i<=5){
    price += 2;
  }
  else if (i<=10) {
    price+=1.5;
  }
  else if (i>10){
    price += 1;
  }
}

System.out.println(price);

(Did this at 3 am in the morning, please forgive if any errors :e:) Edited by Gunstar

Share this post


Link to post
31 minutes ago, ColdMeekly<3 said:

All quoted prices include a fixed call out fee of £10

You nearly nailed it GunStar <3, change int price = 0; to int price = 10

Simple and easy mistake to fix :3

 

Additionally, could you put the answer in spoiler form? :3

Edited by ColdMeekly<3

Share this post


Link to post

This looks fun!  Decided to do a quick one: #Challenge3

2782fe1bcc7cfea3c3b9856bb8da0102.png

67ba83fa3f3704055908be3deb68ec83.png

Just realised forgot to put default case. Meh

Edited by Storm

Share this post


Link to post
10 hours ago, ColdMeekly<3 said:

You nearly nailed it GunStar <3, change int price = 0; to int price = 10

Simple and easy mistake to fix :3

 

Additionally, could you put the answer in spoiler form? :3

oh Lmao, I had it in mind to do that  but forgot. But I would keep the int price =0; and just add a if function to check if number for windows >0 otherwise if user enters 0 or negative number the price would still be 10.

Edited by Gunstar

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...