Demo entry 6660015

python lab x

   

Submitted by anonymous on Nov 13, 2017 at 15:12
Language: Python. Code size: 5.7 kB.

# training2
def box_volume(a, b, c):
"""Calculates the volume of a box that has side lengths a, b and c."""
return(a * b * c)

def fall_time(h):
"""Returns the time (in seconds) needed for an object
falling from a tower of height h (in meters) to hit the ground"""
return(((2 * h) / 9.81) ** 0.5)

def interval_point(a,b,x):
"""Takes three numbers and interprets a and b as the start and end point of
an interval, and x as a fraction between 0 and 1 that determines how
far to go towards b, starting at a"""
return(((b - a) * x) + a)

def impact_velocity(h):
"""Returns the velocity (in metre per second) with which an
object falling from a height of h meters will hit the ground."""
t = ((2 * h) / 9.81) ** 0.5
return(9.81 * t)

def signum(x):
"""A function that returns 1 if x > 0, returns 0 if x = 0, and
returns -1 if x < 0."""
if x > 0:
	return(1)
elif x == 0:
	return(0)
elif x < 0:
	return(-1)

# lab2
def seconds2days(n):
"""Accepts a number of seconds (either as int or float) and converts the
number of seconds in the corresponding number of days."""
return(n / 86400.0)

def box_surface(a, b, c):
"""Computes and returns the surface area of a box with
edge lengths a, b, and c."""
return(2 * ((a * b) + (b * c) + (a * c)))

def triangle_area(a, b, c):
"""Computes and returns the area A of a triangle with
edge lengths a, b, and c."""
s = (a + b + c) / 2.0
x = ((s * (s - a) * (s - b) * (s - c)) ** 0.5)
return(x)

# training3
def degree(x):
"""takes an argument x in radian and returns the corresponding
value in degrees"""
import math
deg = (x * 360) / (2.0 * math.pi)
return deg

def min_max(xs):
"""computes the minimum value xmin of the elements in the list xs,
and the maximum value xmax of the elements in the list, and returns
a tuple (xmin,xmax)"""
a = min(xs)
b = max(xs)
return (a, b)
def geometric_mean(xs):
"""computes the geometric mean of the numbers given in the list xs"""
import numpy
p = numpy.product(xs)
n = len(xs)
return (p ** (1.0 / n))

# lab3
def swing_time(L):
"""Computes and returns the time T [in seconds] needed for an idealized
pendulum of length L [in meters] to complete a single oscillation"""
import math
T = 2 * math.pi * math.sqrt(L / 9.81)
return T

def range_squared(n):
"""Takes a non-negative integer value n and returns the list of the numbers,
up to n-1 squared"""
a = []
for i in range(n):
i = i ** 2
a.append(i)
return a
def count(element, seq):
"""Counts how often a given element 'element' occurs
in the given sequence 'seq', and returns the integer value"""
n = seq.count(element)
return n

# lab4
def seq_sqrt(xs):
"""Takes a list of non-negative numbers xs with elements
[x0, x1, x2, ..., xn], and returns the list [sqrt(x0), sqrt(x1), sqrt(x2),
..., sqrt(xn)]."""
import math
ans = []
for i in xs:
	sqrt = math.sqrt(i)
	ans.append(sqrt)
return ans

def mean(xs):
"""Takes a sequence xs of numbers, and returns
the (arithmetic) mean (i.e. the average value)."""
s = sum(xs)
l = len(xs)
return (float(s) / l)

def wc(filename):
"""Returns the number of words in file filename."""
f = open(filename, 'r')
lines = f.readlines()
f.close()
total = 0
for line in lines:
	words = line.split()
	n = len(words)
	total = total + int(n)
return total

# training5

def count_sub_in_file(filename, s):
"""Takes two arguments: the substring s (of type string) and a filename
of type string). The function then returns the number of occurrences of s in the file given through filename."""
try:
	f = open(filename,'r')
	lines = f.readlines()
	f.close()
except IOError:
	return -1
tot = 0
for line in lines:
	split = str(line.split())
	c = split.count(s)
	tot = tot + c
return tot

def count_vowels(s):
"""A function that returns the number of letters
'a','e','i','o','u','A','E','I','O','U' in a given string s."""
tot = (s.count('a')+s.count('e')+s.count('i')+s.count('o')
+s.count('u')+s.count('A')+s.count('E')+s.count('I')
+s.count('O')+s.count('U'))
return int(tot)

# lab5
def vector_product3(a, b):
"""Takes two sequences of numbers. Both sequence a and sequence b have
three elements. With inputs a=[ax, ay, az] and b=[bx, by, bz], the function
returns a list which contains the vector product of 3d-vectors a and b"""
x1 = a[1] * b[2] - a[2] * b[1]
x2 = a[2] * b[0] - a[0] * b[2]
x3 = a[0] * b[1] - a[1] * b[0]
return [x1, x2, x3]

def seq_mult_scalar(a,s):
"""Which takes a list of numbers a and a scalar (i.e. a number) s.
For the input a=[a0, a1, a2,.., an] the function returns
[s * a0, s * a1, s * a2, ..., s * an]."""
x = []
for i in a:
	n = i * s
	x.append(n)
return x

def powers(n, k):
"""Returns the list [1,n,n^2,n^3,...,n^k] where k is an integer."""
x = 0
a = []
while x < k+1:
	power = n ** x
	x = x + 1
	a.append(power)
return a

def traffic_light(load):
"""takes a floating point number load. The function should return the string:
"green" for values of load below 0.7.
"amber" for values of load equal to or greater than 0.7 but smaller than 0.9
"red" for values of load equal to 0.9 or greater than 0.9"""
if load < 0.7:
	x = str('green')
elif 0.9 > load >= 0.7:
	x = str('amber')
elif load >= 0.9:
	x = str('red')
return x

#lab11
def positive_places(f, xs):
"""Takes a function f and a list of numbers xs, and returns a list of the
numbers for which f(x) is positive"""
ans = []
for i in xs:
if f(i)>0:
ans.append(i)
return ans

def eval_f_0123(f):
"""Evaluates the function f=f(x) at positions x=0, x=1, x=2 and x=3.
Then returns the list [f(0), f(1), f(2), f(3)]."""
ans = []
for i in range(4):
x = f(i)
ans.append(x)
return ans

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).