Example #1
0
# triangle.py: takes three integers as command-line arguments and writes
# True if each one of them is less than or equal to the sum of the other two
# and False otherwise.

import stdio
import sys


# Get x from command line, as an int.

x = int(sys.argv[1])
# Get y from command line, as an int.

y = int(sys.argv[2])
# Get z from command line, as an int.

z = int(sys.argv[3])
# Using a single statement, write True if each of x, y, and z is less than
# or equal to the sum of the other two, and False otherwise.

if (x + y) >= z and (x+z) >= y and (y+z) >= x:
    stdio.writeln(True)
else:
    stdio.writeln(False)
Example #2
0
import sys
import stdio

n = int(sys.argv[1])

total = 0.0
for i in range(1, n + 1):
    # Добавить i-тое слагаемое
    total += 1.0 / i

stdio.writeln(total)
Example #3
0
import sys
import stdio
a = int(sys.argv[1])
b = int(sys.argv[2])
c = int(sys.argv[3])
min = a
if b < min:
    min = b
if c < min:
    min = c
stdio.writeln(min)
Example #4
0
import sys
import stdio

a = int(sys.argv[1])
b = int(sys.argv[2])
max = a
if b > max:
    max = b
stdio.writeln(max)
Example #5
0
import random
import sys

import stdio

n = int(sys.argv[1])
average = 0
min = 1
max = 0
for i in range(n):
    r = random.random()
    average += r
    if r > max: max = r
    if r < min: min = r
    stdio.writeln(r)

stdio.writeln('max = ' + str(max))

stdio.writeln('average = ' + str(average))

stdio.writeln('min = ' + str(min))

# Test 1:
# n = 10
# 0.396299172385971
# 0.15044253958258635
# 0.9815567032652128
# 0.01736829257152528
# 0.07846422275918274
# 0.22480832768917514
# 0.09939782027510391
Example #6
0
import stdio
import sys

# Read integers n1, n2, and float p as command-line arguments.

n1 = int(sys.argv[1])
n2 = int(sys.argv[2])
p = float(sys.argv[3])

# Given probability (q).

q = 1 - p

# Calculate and write the values of P1 and P2, separated by
# a space.

P1 = (1 - (p / q)**n2) / (1 - (p / q)**(n1 + n2))

P2 = (1 - (q / p)**n1) / (1 - (q / p)**(n1 + n2))

stdio.write(P1)
stdio.write(" ")
stdio.writeln(P2)
Example #7
0
# M and N are lengths of x and y respectively.
M = len(x)
N = len(y)
opt = stdarray.create2D(M + 1, N + 1, 0)

# Initialize opt[M][j] (j < N) and opt[i][N] (i < M) to appropriate values.
for j in range(0, N):
    for i in range(0, M):
        opt[M][j] = 2 * (N - j)
        opt[i][N] = 2 * (M - i)

# Compute the rest of opt.
for i in range(M - 1, -1, -1):
    for j in range(N - 1, -1, -1):
        c = opt[i + 1][j] + 2
        if x[i] == y[j]:
            opt[i][j] = min(opt[i + 1][j + 1], c, opt[i][j + 1] + 2)
        else:
            opt[i][j] = min(opt[i + 1][j + 1] + 1, c, opt[i][j + 1] + 2)

# Write x, y, dimensions of opt, and opt.
stdio.writeln(x)
stdio.writeln(y)
stdio.writeln(str(M + 1) + ' ' + str(N + 1))
for i in range(len(opt)):
    for j in range(len(opt[0])):
        if j == len(opt[0]) - 1:
            stdio.writef('%3d\n', opt[i][j])
        else:
            stdio.writef('%3d ', opt[i][j])
Example #8
0
def _regressionTest():
    """
    Perform regression testing.
    """

    clear()

    setPenRadius(.5)
    setPenColor(ORANGE)
    point(0.5, 0.5)
    show(0.0)

    setPenRadius(.25)
    setPenColor(BLUE)
    point(0.5, 0.5)
    show(0.0)

    setPenRadius(.02)
    setPenColor(RED)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(.01)
    setPenColor(GREEN)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(0)
    setPenColor(BLACK)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(.1)
    setPenColor(RED)
    point(0.75, 0.75)
    show(0.0)

    setPenRadius(0)
    setPenColor(CYAN)
    for i in range(0, 100):
        point(i / 512.0, .5)
        point(.5, i / 512.0)
    show(0.0)

    setPenRadius(0)
    setPenColor(MAGENTA)
    line(.1, .1, .3, .3)
    line(.1, .2, .3, .2)
    line(.2, .1, .2, .3)
    show(0.0)

    setPenRadius(.05)
    setPenColor(MAGENTA)
    line(.7, .5, .8, .9)
    show(0.0)

    setPenRadius(.01)
    setPenColor(YELLOW)
    circle(.75, .25, .2)
    show(0.0)

    setPenRadius(.01)
    setPenColor(YELLOW)
    filledCircle(.75, .25, .1)
    show(0.0)

    setPenRadius(.01)
    setPenColor(PINK)
    rectangle(.25, .75, .1, .2)
    show(0.0)

    setPenRadius(.01)
    setPenColor(PINK)
    filledRectangle(.25, .75, .05, .1)
    show(0.0)

    setPenRadius(.01)
    setPenColor(DARK_RED)
    square(.5, .5, .1)
    show(0.0)

    setPenRadius(.01)
    setPenColor(DARK_RED)
    filledSquare(.5, .5, .05)
    show(0.0)

    setPenRadius(.01)
    setPenColor(DARK_BLUE)
    polygon([.4, .5, .6], [.7, .8, .7])
    show(0.0)

    setPenRadius(.01)
    setPenColor(DARK_GREEN)
    setFontSize(24)
    text(.2, .4, 'hello, world')
    show(0.0)

    import picture as p
    pic = p.Picture('mandrill.png')
    picture(pic, .5, .85)
    show(0.0)
    
    # Test handling of mouse and keyboard events.
    setPenColor(BLACK)
    import stdio
    stdio.writeln('Left click with the mouse or type a key')
    while True:
        if mousePressed():
            filledCircle(mouseX(), mouseY(), .02)
        if hasNextKeyTyped():
            stdio.write(nextKeyTyped())
        show(0.0)
        
    # Never get here.
    show()
Example #9
0
import stdio
import sys

m1 = float(sys.argv[1])
m2 = float(sys.argv[2])
r = float(sys.argv[3])

F = 6.674e-11 * ((m1 * m2) / r ** 2)
stdio.writeln(F)
Example #10
0
import stdio
import sys

# Get name1 from command line.
name1 = sys.argv[1]

# Get name2 from command line.
name2 = sys.argv[2]

# Get name3 from command line.
name3 = sys.argv[3]

# Write the output 'Hi name3, name2, and name1.'.
stdio.writeln('Hi ' + name3 + ', ' + name2 + ', and ' + name1 + '.')
def _main():
    dna = sys.argv[1]
    stdio.writeln(wc_complement(dna.upper()))
Example #12
0
import sys
import math
import stdio

x, y = map(float, sys.argv[1:3])

stdio.writeln(math.sqrt(x**2 + y**2))
Example #13
0
def main():
    a = stdio.readAllStrings()
    sort(a)
    for s in a:
        stdio.write(s + ' ')
    stdio.writeln()
Example #14
0
import sys
import stdio

stdio.writeln("Hello " + sys.argv[3] + ", " + sys.argv[2] + " and " +
              sys.argv[1] + "!")
Example #15
0
import stdio
n= stdio.readInt()
a=0
t=n
while (t>=1):
	a=a*10+t%10
	t=(t-t%10)/10	
if (a==n):
	stdio.writeln(str(n) + 'la so doi xung') 
else :
	stdio.writeln(str(n) + 'la so ko doi xung')

Example #16
0
def main():
    isOpen = stdarray.readBool2D()
    stdarray.write2D(flow(isOpen))
    stdio.writeln(percolates(isOpen))
Example #17
0
def _main():
    s = sys.argv[1]
    stdio.writeln(is_palindrome(s))
Example #18
0
import sys
import stdio
stdio.writeln('nhung bien se xe mang di dau gia:')
for a in range(1000, 99999):
    prime = True
    for i in range(2, a):
        if a % i == 0:
            prime = False
    if prime:
        temp = a
        sdn = 0
        while a != 0:
            sdn = sdn * 10 + a % 10
            a = a // 10
            if temp == sdn:
                stdio.write(str(temp) + '  ')
Example #19
0
import sys
import math
import stdio

t = float(sys.argv[1])

stdio.writeln("sin(2*{}) + sin(3*{}) = {}".format(
    t, t,
    math.sin(2 * t) + math.sin(3 * t)))
Example #20
0
import sys
import stdio

stdio.writeln('Nhap so can kiem tra: ')
n = int(stdio.readInt())

i = 2
if n < 2:
    stdio.writeln('%d khong phai la so nguyen to' % n)
elif n == 2:
    stdio.writeln('2 la so nguyen to')
else:
    for i in range(i, n):

        if n % i == 0:

            print('%d khong phai la so nguyen to' % n)
            break
    else:
        print('%d la so nguyen to' % n)
import sys

import stdio

# Accept positive integer n as a command-line argument. Write to
# standard output a table showing the first n powers of two.

n = int(sys.argv[1])

power = 1
i = 0
while i <= n:
    # Write the ith power of 2.
    stdio.writeln(str(i) + ' ' + str(power))
    power = 2 * power
    i = i + 1

# -----------------------------------------------------------------------

# python powersoftwo.py 0
# 0 1

# python powersoftwo.py 1
# 0 1
# 1 2

# python powersoftwo.py 2
# 0 1
# 1 2
# 2 4
Example #22
0
def main():
    x = float(sys.argv[1])
    y = float(sys.argv[2])
    c = Charge(.51, .63, 21.3)
    stdio.writeln(c)
    stdio.writeln(c.potentialAt(x, y))
#Kiem tra xem uoc nao la lon nhat va xem so do co phai so nguyen to khong neu khong phai thi kiem tra uoc tiep theo
import stdio
import math
n = stdio.readInt()
k = int(n / 2) + 1
while k >= 1:
    if n % k == 0:
        flag = True
        for i in range(2, int(math.sqrt(k)) + 1):
            if k % i == 0:
                flag = False
                break
        if flag == True:
            stdio.writeln(k)
            break
    k = k - 1
Example #24
0
def _main():
    x = math.radians(float(sys.argv[1]))
    stdio.writeln(sin(x))
    stdio.writeln(math.sin(x))
Example #25
0
import stdio
import sys
n1 = int(sys.argv[1])
n2 = int(sys.argv[2])
p = float(sys.argv[3])
q = 1.0 - p
pq = p / q

P1 = (1 - pq**n2) / (1 - pq**(n1 + n2))
P2 = (1 - (1 / pq)**n1) / (1 - (1 / pq)**(n1 + n2))
stdio.writeln(str(P1) + ' ' + str(P2))
Example #26
0
# compute the transition matrix. Assume that there are no pages that
# have no outlinks in the input.

n = stdio.readInt()

linkCounts = stdarray.create2D(n, n, 0)
outDegrees = stdarray.create1D(n, 0)

while not stdio.isEmpty():
    # Accumulate link counts.
    i = stdio.readInt()
    j = stdio.readInt()
    outDegrees[i] += 1
    linkCounts[i][j] += 1

stdio.writeln(str(n) + ' ' + str(n))

for i in range(n):
    # Print probability distribution for row i.
    for j in range(n):
        # Print probability for column j.
        if j == 23:
            outDegrees[i] += 1
            linkCounts[i][j] += 1
        p = (.90 * linkCounts[i][j] / outDegrees[i]) + (.10 / n)
        stdio.writef('%8.5f', p)
    stdio.writeln()

#-----------------------------------------------------------------------

# python transition.py < tiny.txt
Example #27
0
import stdio
import math
n = stdio.readInt()
dem = 0
i = 2
while dem != n:
    flag = True
    for j in range(2, int(math.sqrt(i)) + 1):
        if i % j == 0:
            flag = False
            break
    if flag == True:
        dem = dem + 1
    i = i + 1
stdio.writeln(i - 1)
Example #28
0
import random
import sys
import stdio

stake = int(sys.argv[1])
goal = int(sys.argv[2])
trials = int(sys.argv[3])

bets = 0
wins = 0
for t in range(trials):
    cash = stake
    while (cash > 0) and (cash < goal):
        bets += 1
        if random.randrange(0, 2) == 0:
            cash += 1
        else:
            cash -= 1
    if cash == goal:
        wins += 1

stdio.writeln(str(100 * wins // trials) + '% wins')
stdio.writeln('Avg # bets: ' + str(bets // trials))
Example #29
0
# Программа 1.3.6. Метод Ньютона
import sys
import stdio

c = float(sys.argv[1])
EPSILON = 1e-15
t = c
while abs(t - c / t) > EPSILON * t:
    t = (c / t + t) / 2.0
stdio.writeln(t)
Example #30
0
import stdio

stdio.writeln(2 + 3)
# сумма чисел
stdio.writeln(2.2 + 3.3)
# сумма чисел
stdio.writeln('2' + '3')
# соединение строк
stdio.writeln('2.2' + '3.3')
# соединение строк
stdio.writeln(str(2) + str(3))
# перевод int в str и соединение строк
stdio.writeln(str(2.2) + str(3.3))
# перевод int в str и соединение строк
stdio.writeln(int('2') + int('3'))
# перевод str в int и сумма
stdio.writeln(int('2' + '3'))
# соединение строк и перево из str в int
stdio.writeln(float('2') + float('3'))
# перевод в вещественные числа и их сумма
stdio.writeln(float('2' + '3'))
# соеднение строк и перевод в вещественные числа
stdio.writeln(int(2.6 + 2.6))
# сумма вещественных чисел и перевод их в int
stdio.writeln(int(2.6) + int(2.6))
# перевод чисел в int и их сумма