mirror of
https://github.com/ossu/computer-science.git
synced 2024-10-01 01:26:01 -04:00
Remove old files
This commit is contained in:
parent
f8ae3df7e5
commit
b49c27c6a6
@ -1,45 +0,0 @@
|
||||
# 01. Introduction to Computer Science and Programming
|
||||
|
||||
➫ [Link to the original course](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/index.htm)
|
||||
|
||||
## Syllabus
|
||||
|
||||
### About This Course
|
||||
|
||||
Strategic goals of this course:
|
||||
|
||||
* Help students (who may or may not intend to major in computer science) to feel justifiably confident of their ability to write small programs.
|
||||
* Map scientific problems into computational frameworks.
|
||||
* Position students so that they can compete for jobs by providing competence and confidence in computational problem solving.
|
||||
* Prepare college freshmen and sophomores who have no prior programming experience or knowledge of computer science for an easier entry into computer science or electrical engineering majors.
|
||||
* Prepare students from other majors to make profitable use of computational methods in their chosen field.
|
||||
|
||||
This course can be summarized with these six major topics or objectives:
|
||||
|
||||
* Learning a language for expressing computations—Python
|
||||
* Learning about the process of writing and debugging a program
|
||||
* Learning about the process of moving from a problem statement to a computational formulation of a method for solving the problem
|
||||
* Learning a basic set of "recipes"—algorithms
|
||||
* Learning how to use simulations to shed light on problems that don't easily succumb to closed form solutions
|
||||
* Learning about how to use computational tools to help model and understand data
|
||||
|
||||
This course is designed to help you become skillful at making the computer do what you want it to do. Once you acquire this skill, your first instinct when confronted with many tasks will be to write a program to do the task for you. Said another way, we want to help you learn to apply computational modes of thought to frame problems, and to guide the process of deducing information in a computational manner.
|
||||
|
||||
This means that the primary knowledge you will take away from this course is the art of computational problem solving. Unlike many introductory level courses, having an ability to memorize facts will be of little help. This course is about learning to solve problems, not learning facts.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
### UNIT 1
|
||||
|
||||
||Lesson|Archives|
|
||||
|---|---|:---:|
|
||||
|01|INTRODUCTION|[files](https://github.com/ericdouglas/MIT-computer-science/tree/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/01-introduction)|
|
||||
|02|CORE ELEMENTS OF A PROGRAM|[files](https://github.com/ericdouglas/MIT-computer-science-and-engineering/tree/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/02-core-elements-of-a-program)|
|
||||
|03|PROBLEM SOLVING|[files](https://github.com/ericdouglas/MIT-computer-science-and-engineering/tree/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving)|
|
||||
|04|MACHINE INTERPRETATION OF A PROGRAM|[files](https://github.com/ericdouglas/MIT-computer-science-and-engineering/tree/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/04-machine-interpretation-of-program)|
|
||||
|05|OBJECTS IN PYTHON|[files]()|
|
||||
|06|RECURSION|[files]()|
|
||||
|07|DEBUGGING|[files]()|
|
||||
|08|EFFICIENCY AND ORDER OF GROWTH|[files]()|
|
||||
|09|MEMORY AND SEARCH METHODS|[files]()|
|
||||
|10|QUIZ 1|[files]()|
|
@ -1,33 +0,0 @@
|
||||
# 1.1.1 Introduction
|
||||
|
||||
⬅ [Back to table of contents](https://github.com/ericdouglas/MIT-computer-science/tree/master/archives/01-introduction-to-computer-science-and-programming#table-of-contents)
|
||||
|
||||
## Session Overview
|
||||
|
||||
This lecture covers course expectations, introduces computer programming and its uses, and begins to familiarize you with concepts related to how programs work.
|
||||
|
||||
## Session Activities
|
||||
|
||||
### Lecture Videos
|
||||
|
||||
* [Lecture 1: Introduction to 6.00](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-1-introduction-to-6.00/#?w=535) (00:41:28)
|
||||
* **About this video**: Topics covered: Purposes of the course, declarative and imperative knowledge, flow of control, algorithms, fixed program and stored program computers, termination conditions, interpretation, compilation, syntax, static semantics, semantics, and types of errors.
|
||||
* Resources
|
||||
* [Lecture slides (PDF)](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-1-introduction-to-6.00/MIT6_00SCS11_lec01_slides.pdf)
|
||||
|
||||
## Check Yourself
|
||||
|
||||
#### What is the difference between declarative and imperative knowledge?
|
||||
|
||||
Declarative knowledge is statements of fact; imperative knowledge is "how to" knowledge.
|
||||
|
||||
##### What is the advantage of a stored-program computer?
|
||||
|
||||
It's far more versatile than a fixed-program computer, since it interprets a program given to it and carries out those instructions, as opposed to being to do one thing.
|
||||
|
||||
#### What are syntax, static semantics, and semantics of a language?
|
||||
|
||||
Syntax determines whether a string is legal, static semantics determine whether the string has meaning, and semantics assigns a meaning to a legal sentence (assuming no static semantic errors).
|
||||
|
||||
#### What sorts of errors can occur in a program?
|
||||
It can crash, run forever, or give a wrong answer.
|
@ -1,48 +0,0 @@
|
||||
# 1.1.2 Core Elements of a Program
|
||||
|
||||
⬅ [Back to table of contents](https://github.com/ericdouglas/MIT-computer-science/tree/master/archives/01-introduction-to-computer-science-and-programming#table-of-contents)
|
||||
|
||||
This lecture covers the building blocks of straight line and branching programs: objects, types, operators, variables, execution, and conditional statements.
|
||||
|
||||
## Session Activities
|
||||
|
||||
➭ [Lecture 2: Core Elements of a Program](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-2-core-elements-of-a-program/#?w=535) (00:49:49)
|
||||
|
||||
➭ **About this Video**: Topics covered: IDLE, types of objects, operators, overloading, commands, variables, assignment, input, straight line and branching programs, looping constructs, Turing completeness, conditionals, nesting.
|
||||
|
||||
➭ **Resources**
|
||||
|
||||
◈ [Code in Node.JS](https://github.com/ericdouglas/MIT-computer-science-and-engineering/blob/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/02-core-elements-of-a-program/example.js)
|
||||
|
||||
◈ [Code in Python](https://github.com/ericdouglas/MIT-computer-science-and-engineering/blob/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/02-core-elements-of-a-program/example.py)
|
||||
|
||||
## Check Yourself
|
||||
|
||||
### What is a *type*?
|
||||
|
||||
Type are classifications of objects, which is what Python, as an OOP language, deals with. They determine how those objects are dealt with (for example, adding two integers results in an integer, two strings results in a concatenated string, and on integer and a string results in an error).
|
||||
|
||||
### What is an 'expression'?
|
||||
|
||||
An expression is composed of objects (or operands) and operators, and can be interpreted into a value.
|
||||
|
||||
### What is a type conversion?
|
||||
|
||||
A type conversion turns one type of object into another. For example, applying str to the integer 3 results in the string '3'.
|
||||
|
||||
### What is a keyword?
|
||||
|
||||
Keywords are words that have special meanings within a language. Many editors will display them in special colors. These words cannot be used as variables.
|
||||
|
||||
### What is the difference between a straight line program and a branching program?
|
||||
|
||||
A straight line program simply goes through and carries out each step. A branching program will do different things depending on conditions set within the program.
|
||||
|
||||
### What is a conditional?
|
||||
|
||||
A conditional statement starts with an if statement, and can also include elif and else.
|
||||
|
||||
## Problem Sets
|
||||
|
||||
* [Problem Set 0](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-2-core-elements-of-a-program/MIT6_00SCS11_ps0.pdf)
|
||||
* [Problem Set 0 - resolution](https://github.com/ericdouglas/MIT-computer-science-and-engineering/blob/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/02-core-elements-of-a-program/ps0.py)
|
@ -1,3 +0,0 @@
|
||||
x = 3; // Create variable and assign value 3 to it
|
||||
x = x * x; // Bind x to value 9
|
||||
console.log( x ); // print the x's value
|
@ -1,65 +0,0 @@
|
||||
"""
|
||||
x = 3 # create variable x and assign value 3 to it
|
||||
x = x * x # bind x to value 9
|
||||
print x
|
||||
y = raw_input('enter a number: ')
|
||||
print type(y)
|
||||
print y
|
||||
y = float(raw_input('Enter a number: '))
|
||||
print type(y)
|
||||
print y
|
||||
print y * y
|
||||
"""
|
||||
"""
|
||||
x = int(raw_input('Enter a integer: '))
|
||||
if x % 2 == 0:
|
||||
print 'Even'
|
||||
else:
|
||||
print 'Odd'
|
||||
if x % 3 != 0:
|
||||
print 'And not divisible by 3'
|
||||
"""
|
||||
|
||||
"""
|
||||
x = int(raw_input('Enter x: '))
|
||||
y = int(raw_input('Enter y: '))
|
||||
z = int(raw_input('Enter z: '))
|
||||
"""
|
||||
|
||||
"""
|
||||
if x < y:
|
||||
if x < z:
|
||||
print 'x is least'
|
||||
else:
|
||||
print 'z is least'
|
||||
else:
|
||||
print 'y is least'
|
||||
|
||||
if x < y:
|
||||
if x < z:
|
||||
print 'x is least'
|
||||
elif y < z:
|
||||
print 'y is least'
|
||||
else:
|
||||
print 'z is least'
|
||||
"""
|
||||
"""
|
||||
if x < y and x < z:
|
||||
print 'x is least'
|
||||
elif y < z:
|
||||
print 'y is least'
|
||||
else:
|
||||
print 'z is least'
|
||||
"""
|
||||
|
||||
# Find the cube root of a perfect cube
|
||||
x = int(raw_input('Enter an integer: '))
|
||||
ans = 0
|
||||
while ans * ans * ans < abs(x):
|
||||
ans += 1
|
||||
# print 'current guess =', ans
|
||||
if ans * ans * ans != abs(x):
|
||||
print x, 'is not a perfect cube'
|
||||
else:
|
||||
if x < 0:
|
||||
print 'Cube root of ' + str(x) + ' is' + str(ans)
|
@ -1,8 +0,0 @@
|
||||
# Problem Set 0
|
||||
# Name: Eric Douglas
|
||||
# Time Spent: 3:10
|
||||
|
||||
birthday = raw_input('When is your birthday?\n')
|
||||
lastName = raw_input('What is your last name?\n')
|
||||
|
||||
print lastName + ' ' + birthday
|
Binary file not shown.
@ -1,32 +0,0 @@
|
||||
# 1.1.3 Problem Solving
|
||||
|
||||
⬅ [Back to table of contents](https://github.com/ericdouglas/MIT-computer-science/tree/master/archives/01-introduction-to-computer-science-and-programming#table-of-contents)
|
||||
|
||||
## Session Overview
|
||||
|
||||
This lecture covers the use of iteration to build programs whose execution time depends upon the size of inputs. It also introduces search problems and brute force and bisection for solving them.
|
||||
|
||||
## Session Activities
|
||||
|
||||
### Lecture Videos
|
||||
|
||||
* [Lecture 3: Problem Solving](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-3-problem-solving/#?w=535) (00:47:56)
|
||||
* **About this video**: Topics covered: Termination, decrementing functions, exhaustive enumeration, brute force, while loop, for loop, approximation, specifications, bisection search.
|
||||
* **Resources**
|
||||
* [code in Pyhton](https://github.com/ericdouglas/MIT-computer-science-and-engineering/blob/master/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/lec03.py)
|
||||
|
||||
## Check Yourself
|
||||
|
||||
### **What does it mean for a program to terminate?**
|
||||
|
||||
Either the program will return a value, or throw an exception. A program that does not terminate runs indefinitely, typically because it's gotten stuck in a loop.
|
||||
|
||||
### What is a `for` loop?
|
||||
|
||||
A `for` loop takes some sort of iterable object (a list, tuple, or string) and performs its function once for each item in that object. Any function that depends on the input can have a different result at each step, since the input is the current item.
|
||||
|
||||
## Further Study
|
||||
|
||||
**Readings**
|
||||
* [Loops](http://beastie.cs.ua.edu/cs150/book/index_14.html). An Introduction to Python.
|
||||
|
@ -1,84 +0,0 @@
|
||||
"""
|
||||
# Find the cube root of a perfect cube
|
||||
x = int(raw_input('Enter an integer: '))
|
||||
ans = 0
|
||||
|
||||
while ans * ans * ans < abs(x):
|
||||
ans = ans + 1
|
||||
# print 'current guess = ', ans
|
||||
|
||||
if ans * ans * ans != abs(x):
|
||||
print x, ' is not a perfect cube'
|
||||
else:
|
||||
if x < 0:
|
||||
ans = -ans
|
||||
print 'Cube root of ' + str(x) + ' is ' + str(ans)
|
||||
"""
|
||||
|
||||
"""
|
||||
# Find the cube root of a perfect cube
|
||||
x = int(raw_input('Enter an integer: '))
|
||||
for ans in range(0, abs(x) + 1):
|
||||
if ans**3 == abs(x):
|
||||
break
|
||||
if ans**3 != abs(x):
|
||||
print x, ' is not a perfect cube'
|
||||
else:
|
||||
if x < 0:
|
||||
ans = -ans
|
||||
print 'Cube root of ' + str(x) + ' is ' + str(ans)
|
||||
"""
|
||||
|
||||
"""
|
||||
x = 25000
|
||||
epsilon = 0.01
|
||||
numGuesses = 0
|
||||
ans = 0.0
|
||||
|
||||
while abs(ans**2 - x) >= epsilon and ans <= x:
|
||||
ans += 0.00001
|
||||
numGuesses += 1
|
||||
|
||||
print 'numGuesses = ', numGuesses
|
||||
|
||||
if abs(ans**2 - x) >= epsilon:
|
||||
print 'Failed on square root of ', x
|
||||
else:
|
||||
print ans, ' is close to square root of ', x
|
||||
"""
|
||||
"""
|
||||
x = 12345
|
||||
epsilon = 0.01
|
||||
numGuesses = 0
|
||||
low = 0.0
|
||||
high = x
|
||||
ans = (high + low) / 2.0
|
||||
|
||||
while abs(ans ** 2 - x) >= epsilon and ans <= x:
|
||||
#print low, high, ans
|
||||
numGuesses += 1
|
||||
if ans ** 2 < x:
|
||||
low = ans
|
||||
else:
|
||||
high = ans
|
||||
ans = (high + low) / 2.0
|
||||
|
||||
print 'numGuesses = ', numGuesses
|
||||
print ans, ' is close to square root of ', x
|
||||
"""
|
||||
|
||||
def withinEpsilon(x, y, epsilon):
|
||||
""" x, y, epsilon all ints or floats
|
||||
returns true if x is within epsilon of y """
|
||||
|
||||
return abs(x - y) <= epsilon
|
||||
|
||||
if withinEpsilon(25, 26, 1):
|
||||
print 'yes'
|
||||
else:
|
||||
print 'no'
|
||||
|
||||
if withinEpsilon(25, 26, 0.9):
|
||||
print 'yes'
|
||||
else:
|
||||
print 'no'
|
@ -1,176 +0,0 @@
|
||||
# Loops - http://beastie.cs.ua.edu/cs150/book/index_14.html
|
||||
|
||||
i = 0
|
||||
while i < 5:
|
||||
print i
|
||||
i += 1
|
||||
|
||||
# Other loops
|
||||
for i in range(0, 5, 1): # 5 exclusive
|
||||
print i
|
||||
|
||||
# The counting pattern
|
||||
count = 0
|
||||
for i in range(0, len(items), 1):
|
||||
count += 1
|
||||
|
||||
# The filtered-count pattern
|
||||
count = 0
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] % 2 == 0:
|
||||
count += 1
|
||||
|
||||
# The accumulate pattern
|
||||
total = 0
|
||||
for i in range(0, len(items), 1):
|
||||
total += items[i]
|
||||
|
||||
# The filtered-accumulate pattern
|
||||
def sumEvens(items):
|
||||
total = 0
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] % 2 == 0:
|
||||
total += items[i]
|
||||
return total
|
||||
|
||||
# The search pattern
|
||||
def find(target, items): # is target in items?
|
||||
return occurrences(target, items) > 0
|
||||
|
||||
def occurrences(target, items):
|
||||
count = 0
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] == target:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
## search with short-circuit
|
||||
def find(target, items):
|
||||
for i in range(0, len(items), 1):
|
||||
if items[i] == target:
|
||||
return True
|
||||
return False
|
||||
|
||||
# The extreme pattern
|
||||
largest = items[0]
|
||||
for i in range(1, len(items), 1): # start comparing at index 1
|
||||
if items[i] > largest:
|
||||
largest = items[i]
|
||||
|
||||
# The filter pattern
|
||||
def extractEvens(items):
|
||||
for i in range(0, len(items), 1):
|
||||
if isEven(items[i]):
|
||||
evens = evens + [items[i]] # array concatenation
|
||||
return evens
|
||||
|
||||
## Add isEven function
|
||||
def isEven(value):
|
||||
if value % 2 == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
someValues = [1,2,4,3,5,7,8,6,9]
|
||||
|
||||
print extractEvens(someValues)
|
||||
|
||||
## Using append method because array concatenation is very slowly for large arrays
|
||||
def extractEvens(items):
|
||||
evens = []
|
||||
for i in range(0, len(items), 1):
|
||||
if isEven(items[i]):
|
||||
evens.append(items[i]) # method invocation
|
||||
return evens
|
||||
|
||||
# The map pattern
|
||||
def map(f, items):
|
||||
result = []
|
||||
for i in range(0, len(items), 1):
|
||||
transformed = f(items[i])
|
||||
result.append(transformed)
|
||||
return result
|
||||
|
||||
## Using this pattern
|
||||
def increment(value):
|
||||
return value + 1
|
||||
|
||||
someValues = [1, 2, 3, 4, 5, 6, 7]
|
||||
print map(increment, someValues)
|
||||
|
||||
# The shuffle pattern
|
||||
## arrays with the same size
|
||||
def shuffle(array1, array2):
|
||||
array3 = []
|
||||
for i in range(0, len(array1), 1):
|
||||
array3.append(array1[i])
|
||||
array3.append(array2[i])
|
||||
return array3
|
||||
|
||||
## arrays without the same size
|
||||
def shuffle2(array1, array2):
|
||||
array3 = []
|
||||
if len(array1) < len(array2):
|
||||
for i in range(len(array1), 1):
|
||||
array3.append(array1[i])
|
||||
array3.append(array2[i])
|
||||
return array3 + array2[i+1:]
|
||||
else:
|
||||
for i in range(0, len(array2), 1):
|
||||
array3.append(array1[i])
|
||||
array3.append(array2[i])
|
||||
return array3 + array1[i+1:]
|
||||
|
||||
## using a while loop instead
|
||||
def shuffle3(array1, array2):
|
||||
array3 = []
|
||||
i = 0
|
||||
while (i < len(array1) and i < len(array2)):
|
||||
array3.append(array1[i])
|
||||
array3.append(array2[i])
|
||||
i += 1
|
||||
return array3 + array1[i:] + array2[i:]
|
||||
|
||||
## back to shuffle2 but adding recursion
|
||||
def shuffle4(array1, array2):
|
||||
if len(array2) < len(array1):
|
||||
return shuffle4(array2, array1) # arrays are flipped
|
||||
else:
|
||||
array3 = []
|
||||
for i in range(0, len(array1), 1):
|
||||
array3.append(array1[i])
|
||||
array3.append(array2[i])
|
||||
return array3 + array2[i+1:]
|
||||
|
||||
# The merge pattern
|
||||
|
||||
def merge(array1, array2):
|
||||
array3 = []
|
||||
i = 0
|
||||
j = 0
|
||||
while (i < len(array1) and j < len(array2)):
|
||||
if array1[i] < array2[j]:
|
||||
array3.append(array1[i])
|
||||
i += 1
|
||||
else:
|
||||
array3.append(array2[j])
|
||||
j += 1
|
||||
return array3 + array1[i:] + array2[j:]
|
||||
|
||||
## using the merge pattern
|
||||
array1 = [1,3,4,6,8]
|
||||
array2 = [2,5,7,9,10]
|
||||
|
||||
print merge(array1, array2)
|
||||
|
||||
# The fossilized pattern (never end!)
|
||||
|
||||
i = 0
|
||||
while i < n:
|
||||
print(i)
|
||||
|
||||
# The missed-condition pattern (never end)
|
||||
|
||||
i = n
|
||||
while i > 0:
|
||||
print(i)
|
||||
i += 1
|
@ -1,37 +0,0 @@
|
||||
# 1.1.4 Machine Interpretation of a Program
|
||||
|
||||
## Session Overview
|
||||
|
||||
This lecture introduces the notion of decomposition and abstraction by specification. It also covers Python modules, functions, parameters, and scoping. Finally, it uses the Python assert statement and type 'str'.
|
||||
|
||||
## Session Activities
|
||||
|
||||
### Lecture Videos
|
||||
|
||||
* [Lecture 4: Machine Interpretation of a Program](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-4-machine-interpretation-of-a-program/) (00:50:18)
|
||||
* **About this Video** - Topics covered: Decomposition, module, function, abstraction, formal parameter, actual parameter, argument, assert, scope, mapping, stack, last in first out, LIFO, strings, slicing.
|
||||
* **Resources**:
|
||||
* [Lecture Code]()
|
||||
* [Lecture code handout (PDF)](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-4-machine-interpretation-of-a-program/MIT6_00SCS11_lec04.pdf)
|
||||
|
||||
* Recitation Videos
|
||||
* **Recitation 2**: Loops, Tuples, Strings and Functions (00:57:42)
|
||||
* **About this video**: Topics covered: loops, tuples, concatenating tuples and strings, string operations, immutability, range function, slicing, types of data structures, decrementing function, global and local variables, global keepers.
|
||||
|
||||
### Recitation Videos
|
||||
|
||||
* [Recitation 2: Loops, Tuples, Strings and Functions](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-4-machine-interpretation-of-a-program/#?w=535) (00:57:42)
|
||||
|
||||
## Check Yourself
|
||||
|
||||
### What is decomposition?
|
||||
|
||||
Decomposition breaks a problem into self-contained, manageable parts.
|
||||
|
||||
### What is abstraction?
|
||||
|
||||
Abstraction allows us to ignore the details of a piece of code, and use it as a black box - input x, get y.
|
||||
|
||||
### What is the difference between formal and actual parameters?
|
||||
|
||||
Formal parameters are the names of variables used inside a procedure; actual parameters (or arguments) are the values to those names.
|
@ -1,16 +0,0 @@
|
||||
var x = 0.5;
|
||||
var epsilon = 0.01;
|
||||
var low = 0;
|
||||
var high = x;
|
||||
var ans = ( high + low ) / 2;
|
||||
|
||||
while ( Math.abs( Math.pow( ans, 2 ) ) >= epsilon && ans <= x ) {
|
||||
console.log( 'ans = ' + ans + ' low = ' + low + ' high = ' + high );
|
||||
|
||||
if ( Math.pow( ans, 2 ) < x )
|
||||
low = ans;
|
||||
else
|
||||
high = ans;
|
||||
}
|
||||
|
||||
console.log( ans + ' is close to square root of ' + x );
|
@ -1,102 +0,0 @@
|
||||
|
||||
##x = 0.5
|
||||
##epsilon = 0.01
|
||||
##low = 0.0
|
||||
##high = x
|
||||
##ans = (high + low)/2.0
|
||||
##while abs(ans**2 - x) >= epsilon and ans <= x:
|
||||
## print 'ans =', ans, 'low =', low, 'high =', high
|
||||
## if ans**2 < x:
|
||||
## low = ans
|
||||
## else:
|
||||
## high = ans
|
||||
## ans = (high + low)/2.0
|
||||
##print ans, 'is close to square root of', x
|
||||
|
||||
##def withinEpsilon(x, y, epsilon):
|
||||
## """x,y,epsilon ints or floats. epsilon > 0.0
|
||||
## returns True if x is within epsilon of y"""
|
||||
## return abs(x - y) <= epsilon
|
||||
|
||||
##print withinEpsilon(2,3,1)
|
||||
##val = withinEpsilon(2,3,0.5)
|
||||
##print val
|
||||
|
||||
##def f(x):
|
||||
## x = x + 1
|
||||
## print 'x =', x
|
||||
## return x
|
||||
##
|
||||
##x = 3
|
||||
##z = f(x)
|
||||
##print 'z =', z
|
||||
##print 'x =', x
|
||||
|
||||
##def f1(x):
|
||||
## def g():
|
||||
## x = 'abc'
|
||||
## x = x + 1
|
||||
## print 'x =', x
|
||||
## g()
|
||||
## assert False
|
||||
## return x
|
||||
|
||||
##x = 3
|
||||
##z = f1(x)
|
||||
|
||||
##def isEven(i):
|
||||
## """assumes i a positive int
|
||||
## returns True if i is even, otherwise False"""
|
||||
## return i%2 == 0
|
||||
|
||||
##def findRoot(pwr, val, epsilon):
|
||||
## """assumes pwr an int; val, epsilon floats
|
||||
## pwr and epsilon > 0
|
||||
## if it exists,
|
||||
## returns a value within epsilon of val**pwr
|
||||
## otherwise returns None"""
|
||||
## assert type(pwr) == int and type(val) == float\
|
||||
## and type(epsilon) == float
|
||||
## assert pwr > 0 and epsilon > 0
|
||||
## if isEven(pwr) and val < 0:
|
||||
## return None
|
||||
## low = -abs(val)
|
||||
## high = max(abs(val), 1.0)
|
||||
## ans = (high + low)/2.0
|
||||
## while not withinEpsilon(ans**pwr, val, epsilon):
|
||||
## #print 'ans =', ans, 'low =', low, 'high =', high
|
||||
## if ans**pwr < val:
|
||||
## low = ans
|
||||
## else:
|
||||
## high = ans
|
||||
## ans = (high + low)/2.0
|
||||
## return ans
|
||||
|
||||
##def testFindRoot():
|
||||
## """x float, epsilon float, pwr positive int"""
|
||||
## for x in (-1.0, 1.0, 3456.0):
|
||||
## for pwr in (1, 2, 3):
|
||||
## ans = findRoot(pwr, x, 0.001)
|
||||
## if ans == None:
|
||||
## print 'The answer is imaginary'
|
||||
## else:
|
||||
## print ans, 'to the power', pwr,\
|
||||
## 'is close to', x
|
||||
|
||||
#testFindRoot()
|
||||
|
||||
##sumDigits = 0
|
||||
##for c in str(1952):
|
||||
## sumDigits += int(c)
|
||||
##print sumDigits
|
||||
|
||||
##x = 100
|
||||
##divisors = ()
|
||||
##for i in range(1, x):
|
||||
## if x%i == 0:
|
||||
## divisors = divisors + (i,)
|
||||
##
|
||||
##print divisors
|
||||
##print divisors[0] + divisors[1]
|
||||
##print divisors[2:4]
|
||||
|
@ -1,12 +0,0 @@
|
||||
print "hello python!"
|
||||
|
||||
print 4 + 4
|
||||
|
||||
import math
|
||||
print math.sqrt(16)
|
||||
print math.pow(3, 2)
|
||||
print math.cos(0)
|
||||
|
||||
# exercises
|
||||
print math.pow(23, 5)
|
||||
|
Loading…
Reference in New Issue
Block a user