Skip to content

Instantly share code, notes, and snippets.

View mayankdawar's full-sized avatar

Mayank Dawar mayankdawar

  • Ludhiana , Chandigarh , Mohali
View GitHub Profile
@mayankdawar
mayankdawar / str_len.py
Created February 17, 2020 19:04
Write code to count the number of characters in original_str using the accumulation pattern and assign the answer to a variable num_chars. Do NOT use the len function to solve the problem (if you use it while you are working on this problem, comment it out afterward!)
original_str = "The quick brown rhino jumped over the extremely lazy fox."
counter = 0 #intializing a counter varialble
for i in original_str:
counter += 1 #adding 1 after every iteration
num_chars = counter #getting the result
@mayankdawar
mayankdawar / addStringList.py
Created February 17, 2020 19:01
addition_str is a string with a list of numbers separated by the + sign. Write code that uses the accumulation pattern to take the sum of all of the numbers and assigns it to sum_val (an integer). (You should use the .split("+") function to split by "+" and int() to cast to an integer).
addition_str = "2+5+10+20"
lst = addition_str.split('+') #converting string into list
new_lst = [int(i) for i in lst] #converting strings into integer
sum_val = sum(new_lst) #Sum of list
@mayankdawar
mayankdawar / avg_list.py
Created February 17, 2020 18:56
week_temps_f is a string with a list of fahrenheit temperatures separated by the , sign. Write code that uses the accumulation pattern to compute the average (sum divided by number of items) and assigns it to avg_temp. Do not hard code your answer (i.e., make your code compute both the sum or the number of items in week_temps_f) (You should use …
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
temp_list = week_temps_f.split(',') #converting string into a list
lst=[float(i) for i in temp_list] #converting string values of list into float
avg_temp = sum(lst)/len(lst) #calculating average
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class link
{
public: