Problem:


Koch Curve

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

Divide a given segment (p1, p2) into three equal segments.

Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.

Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input

An integer n is given.

Output

Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints

 0 ≤ n ≤ 6

Sample Input 1

1

Sample Output 1

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2

2

Sample Output 2

0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000

Notes



Sample input:
1


Sample output:
0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Submission status: Accepted

Solution code:
#! /usr/local/bin/python3
# coding: utf-8

from math import pi
from cmath import rect

def fromPolar(r, theta):
    return r * complex(cos(theta), sin(theta))

def print_xy(z):
    print(z.real, z.imag)

def koch(p1, p2, level):
    if level == 0:
        return
    l = p2 - p1
    s = p1 + (1/3) * l
    t = p1 + (2/3) * l
    u = s + (1/3) * l * rect(1, pi/3)
    koch(p1, s, level - 1)
    print_xy(s)
    koch(s, u, level - 1)
    print_xy(u)
    koch(u, t, level - 1)
    print_xy(t)
    koch(t, p2, level - 1)

def main():
    n = int(input())
    print_xy(0+0j)
    koch(0+0j, 100+0j, n)
    print_xy(100+0j)

main()

Submission status: Accepted

Solution code:
import math

global th
th = math.pi * (60/180)

def koch(d, p1, p2):
    if d == 0:
        return
    else:
        s_x = (2 * p1[0] + p2[0]) / 3
        s_y = (2 * p1[1] + p2[1]) / 3
        s = (s_x, s_y)
        t_x = (p1[0] + 2 * p2[0]) / 3
        t_y = (p1[1] + 2 * p2[1]) / 3
        t = (t_x, t_y)
        u_x = (t[0] - s[0]) * math.cos(th) - (t[1] - s[1]) * math.sin(th) + s[0]
        u_y = (t[0] - s[0]) * math.sin(th) + (t[1] - s[1]) * math.cos(th) + s[1]
        u = (u_x, u_y)

        koch(d-1, p1, s)
        print(*s, sep=" ")
        koch(d-1, s, u)
        print(*u, sep=" ")
        koch(d-1, u, t)
        print(*t, sep=" ")
        koch(d-1, t, p2)

n = int(input())
p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
print(*p1, sep=" ")
koch(n, p1, p2)
print(*p2, sep=" ")

Submission status: Accepted

Solution code:
import math
n = int(input())

def fractal(A,B):
    global a1
    global a2
    global a3
    x1 = A[0]
    y1 = A[1]
    x2 = B[0]
    y2 = B[1]
    a1 = [(2*x1 + x2)/3, (2*y1 + y2)/3]
    a3 = [(x1 + 2*x2)/3, (y1 + 2*y2)/3]
    X = a3[0] - a1[0]
    Y = a3[1] - a1[1]
    a2 = [a1[0] + X/2 - Y*math.sqrt(3)/2, a1[1] + X*math.sqrt(3)/2 + Y/2]
    
A = [0,0]
B = [100,0]
fract = [A,B]
for i in range(n):
    l = len(fract)
    f = list(fract)
    for k in range(l-1):
        fractal(f[k], f[k+1])
        fract.insert(4*k+1,a1)
        fract.insert(4*k+2,a2)
        fract.insert(4*k+3,a3)        
for i in fract:
    print('{0:.6f}'.format(i[0]) + ' {0:.6f}'.format(i[1]))
#print(fract)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

def koch(n, sx, sy, gx, gy):
    if n == 0:
        print gx, gy
        return
    vx = gx - sx
    vy = gy - sy
    x1 = sx + vx / 3
    y1 = sy + vy / 3
    x3 = gx - vx / 3
    y3 = gy - vy / 3
    vx60 = vx / 2 - math.sqrt(3) * vy / 2
    vy60 = math.sqrt(3) * vx / 2 + vy / 2
    x2 = x1 + vx60 / 3
    y2 = y1 + vy60 / 3
    koch(n - 1, sx, sy, x1, y1)
    koch(n - 1, x1, y1, x2, y2)
    koch(n - 1, x2, y2, x3, y3)
    koch(n - 1, x3, y3, gx, gy)

n = input()
print 0, 0
koch(n, 0.0, 0.0, 100.0, 0.0)

Submission status: Accepted

Solution code:
import math
import os
import sys


def kochCurve(depth, p0, p1):

    if depth == 0:
        return

    sx = 2 / 3 * p0[0] + 1 / 3 * p1[0]
    sy = 2 / 3 * p0[1] + 1 / 3 * p1[1]
    s = (sx, sy)

    tx = 1 / 3 * p0[0] + 2 / 3 * p1[0]
    ty = 1 / 3 * p0[1] + 2 / 3 * p1[1]
    t = (tx, ty)

    angle = math.radians(60)

    ux = math.cos(angle) * (tx - sx) - math.sin(angle) * (ty - sy) + sx
    uy = math.sin(angle) * (tx - sx) + math.cos(angle) * (ty - sy) + sy
    u = (ux, uy)

    kochCurve(depth - 1, p0, s)
    print(*s)
    kochCurve(depth - 1, s, u)
    print(*u)
    kochCurve(depth - 1, u, t)
    print(*t)
    kochCurve(depth - 1, t, p1)


n = int(input())
p0 = (0, 0)
p1 = (100, 0)

print(*p0)
kochCurve(n, p0, p1)
print(*p1)

Submission status: Accepted

Solution code:
import math

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y


def koch(n, a, b):
    if n == 0:
        return

    th = math.pi * 60 / 180
    s = Point((2*a.x + b.x) / 3, (2*a.y + b.y) / 3)
    t = Point((a.x + 2*b.x) / 3, (a.y + 2*b.y) / 3)
    u = Point((t.x-s.x) * math.cos(th) - (t.y-s.y) * math.sin(th) + s.x,
              (t.x-s.x) * math.sin(th) + (t.y-s.y) * math.cos(th) + s.y)

    koch(n-1, a, s)
    print('{0:.8f} {1:.8f}'.format(s.x, s.y))
    koch(n-1, s, u)
    print('{0:.8f} {1:.8f}'.format(u.x, u.y))
    koch(n-1, u, t)
    print('{0:.8f} {1:.8f}'.format(t.x, t.y))
    koch(n-1, t, b)

n = int(input())
a = Point(0, 0)
b = Point(100, 0)

print('{0:.8f} {1:.8f}'.format(a.x, a.y))
koch(n, a, b)
print('{0:.8f} {1:.8f}'.format(b.x, b.y))

Submission status: Accepted

Solution code:
from math import sin, cos, pi
def koch(px, py, qx, qy, n):
    if n == 0:
        return True
    sx = (2 * px + qx) / 3
    sy = (2 * py + qy) / 3
    tx = (px + 2 * qx) / 3
    ty = (py + 2 * qy) / 3
    ux = sx + (tx - sx) * cos(pi / 3) - (ty - sy) * sin(pi / 3)
    uy = sy + (tx - sx) * sin(pi / 3) + (ty - sy) * cos(pi / 3)
    koch(px, py, sx, sy, n - 1)
    print("{:.8f} {:.8f}".format(sx, sy))
    koch(sx, sy, ux, uy, n - 1)
    print("{:.8f} {:.8f}".format(ux, uy))
    koch(ux, uy, tx, ty, n - 1)
    print("{:.8f} {:.8f}".format(tx, ty))
    koch(tx, ty, qx, qy, n - 1)
n = int(input())
print("{:.8f} {:.8f}".format(0, 0))
koch(0, 0, 100, 0, n)
print("{:.8f} {:.8f}".format(100, 0))

Submission status: Accepted

Solution code:
def next_koch(n):
    turn60 = 0.5 + 3 ** 0.5 * 0.5j
    n_next = [n[0]]
    for i in range(1, len(n)):
        s = (n[i - 1] * 2 + n[i]) / 3
        t = (n[i - 1] + n[i] * 2) / 3
        u = (t - s) * turn60 + s
        n_next +=[s, u, t, n[i]]
    return n_next
 
p1 = 0 + 0j
p2 = 100 + 0j
koch = [p1, p2]
 
n = int(input())
for i in range(n):
    koch = next_koch(koch)
 
for z in koch:
    print("{:.8f} {:.8f}".format(z.real, z.imag))

Submission status: Accepted

Solution code:
# Recursion / Divide and Conquer - Koch Curve
import math
cos60, sin60 = math.cos(math.pi/3),math.sin(math.pi/3)
class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return str(self.x)+' '+str(self.y)

def koch_curve(n, p1,p2):
    if n <= 0:
        return
    # Divide a given segment (p1, p2) into three equal segments.
    s = Point((2*p1.x + p2.x) / 3, (2*p1.y + p2.y) / 3)
    t = Point((p1.x + 2*p2.x) / 3, (p1.y + 2*p2.y) / 3)
    # p1 < s < t < p2 ????????£??????
    # u???????????????????????§?±??????????u???t???s????§???????60????????¢???????????????????¬????????????¢??????????????¨??????
    # Replace the middle segment by the two sides of
    # an equilateral triangle (s, u, t) of
    # the same length as the segment.
    ux = (t.x-s.x)*cos60 - (t.y-s.y)*sin60 + s.x
    uy = (t.x-s.x)*sin60 + (t.y-s.y)*cos60 + s.y
    u = Point(ux, uy)
    # Repeat this procedure recursively for
    # new segments (p1, s), (s, u), (u, t), (t, p2).
    koch_curve(n-1,p1,s)
    print(s)
    koch_curve(n-1,s,u)
    print(u)
    koch_curve(n-1,u,t)
    print(t)
    koch_curve(n-1,t,p2)

n = int(input())
ps = Point(0,0)
pe = Point(100,0)
print(ps)
koch_curve(n,ps,pe)
print(pe)

Submission status: Runtime Error

Solution code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	static class Point {
		double x;
		double y;
		public Point(double x, double y) {
			this.x = x;
			this.y = y;
		}
		public String toString() {
			return String.format("%.8f %.8f", x, y);
		}
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(reader.readLine());
		reader.close();		
		getKochCurve(n, new Point(0, 0), new Point(100, 0));
	}

	private static void getKochCurve(int n, Point start, Point end) {
		System.out.println(start);
		getKochCurve(0, n, start, end);
		System.out.println(end);
	}

	private static void getKochCurve(int i, int n, Point start, Point end) {		
		if (i < n) {
			Point firstPoint = getPoint(start, end, 1.0/3.0);
			Point thirdPoint = getPoint(start, end, 2.0/3.0);
			Point secondPoint = getSecondPoint(start, end, firstPoint, thirdPoint);
			getKochCurve(i + 1, n, start, firstPoint);
			System.out.println(firstPoint);
			getKochCurve(i + 1, n, firstPoint, secondPoint);
			System.out.println(secondPoint);
			getKochCurve(i + 1, n, secondPoint, thirdPoint);
			System.out.println(thirdPoint);
			getKochCurve(i + 1, n, thirdPoint, end);
		}
	}

	private static Point getPoint(Point start, Point end, double d) {
		double deltaX = end.x - start.x;
		double deltaY = end.y - start.y;
		deltaX *= d;
		deltaY *= d;
		return new Point(start.x + deltaX, start.y + deltaY);
	}

	private static Point getSecondPoint(Point start, Point end, Point second, Point third) {
		double deltaX = third.x - second.x;
		double deltaY = third.y - second.y;
		double len = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));		
		double x;
		double y;		
		if (deltaY == 0) {
			x = start.x + (end.x - start.x) / 2;
			y = second.y + Math.sqrt((Math.pow(len, 2) - Math.pow(len / 2.0, 2)));
		} else if (start.x < end.x && start.y < end.y) {
			x = start.x;
			y = third.y;
		} else if (start.x < end.x && start.y > end.y) {
			x = end.x;
			y = second.y;
		} else if (start.x > end.x && start.y < end.y) {
			x = end.x;
			y = second.y;
		} else {
			x = start.x;
			y = third.y;
		}
		return new Point(x, y);
	}
}

Submission status: Runtime Error

Solution code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	static class Point {
		double x;
		double y;
		public Point(double x, double y) {
			this.x = x;
			this.y = y;
		}
		public String toString() {
			return String.format("%.8f %.8f", x, y);
		}
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(reader.readLine());
		reader.close();		
		getKochCurve(n, new Point(0, 0), new Point(100, 0));
	}

	private static void getKochCurve(int n, Point start, Point end) {
		System.out.println(start);
		getKochCurve(0, n, start, end);
		System.out.println(end);
	}

	private static void getKochCurve(int i, int n, Point start, Point end) {		
		if (i < n) {
			Point firstPoint = getPoint(start, end, 1.0/3.0);
			Point thirdPoint = getPoint(start, end, 2.0/3.0);
			Point secondPoint = getSecondPoint(start, end, firstPoint, thirdPoint);
			getKochCurve(i + 1, n, start, firstPoint);
			System.out.println(firstPoint);
			getKochCurve(i + 1, n, firstPoint, secondPoint);
			System.out.println(secondPoint);
			getKochCurve(i + 1, n, secondPoint, thirdPoint);
			System.out.println(thirdPoint);
			getKochCurve(i + 1, n, thirdPoint, end);
		}
	}

	private static Point getPoint(Point start, Point end, double d) {
		double deltaX = end.x - start.x;
		double deltaY = end.y - start.y;
		deltaX = deltaX * d;
		deltaY = deltaY * d;				
		return new Point(start.x + deltaX, start.y + deltaY);
	}

	private static Point getSecondPoint(Point start, Point end, Point second, Point third) {
		double deltaX = third.x - second.x;
		double deltaY = third.y - second.y;
		double len = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));		
		double x;
		double y;		
		if (deltaY == 0) {
			x = start.x + (end.x - start.x) / 2;
			if (start.x < end.x) {
				y = second.y + Math.sqrt((Math.pow(len, 2) - Math.pow(len / 2.0, 2)));
			} else {
				y = second.y - Math.sqrt((Math.pow(len, 2) - Math.pow(len / 2.0, 2)));
			}
		} else if (start.x < end.x && start.y < end.y) {
			x = start.x;
			y = third.y;
		} else if (start.x < end.x && start.y > end.y) {
			x = end.x;
			y = second.y;
		} else if (start.x > end.x && start.y < end.y) {
			x = end.x;
			y = second.y;
		} else {
			x = start.x;
			y = third.y;
		}
		return new Point(x, y);
	}
}

Submission status: Runtime Error

Solution code:
import math

n = int(raw_input())

th = math.pi * 60.0 / 180.0

def split(x,y):
    s = ((2*x[0]+y[0])/3, (2*x[1]+y[1])/3)
    t = ((x[0]+2*y[0])/3, (x[1]+2*y[1])/3)
    u = ((t[0]-s[0])*math.cos(th) - (t[1]-s[1])*math.sin(th) + s[0], (t[0]-s[0])*math.sin(th) + (t[1]-s[1])*math.cos(th) + s[1])
    return (s,u,t)


s = (0.,0.)
t = (100.,0.)
points = []
points.append(s)
points.append(t)
for i in range(n):
    new_points = []
    j = 0
    while True:
        s,u,t = split(points[j],points[j+1])
        new_points.extend((points[j],s,u,t,points[j+1]))
        j += 1
        if j+1 >= len(points): break
    points = new_points
for p in new_points:
    print p[0],p[1]

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s.x = (2*a.x* + b.x)/3
  s.y = (2*a.y* + b.y)/3
  t.x = (a.x* + 2*b.x)/3
  t.y = (a.y* + 2*b.y)/3
  u.x = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  u.y = (t.x - s.x)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y

  koch(n-1, a, s)
  print  "%f %f" %(s.x, s.y)
  koch(n-1, s, u)
  print  "%f %f" %(u.x, u.y)
  koch(n-1, u, t)
  print  "%f %f" %(t.x, t.y)
  koch(n-1, t, b)

n = input()

p1.x = 0
p1.y = 0
p2.x = 100
p2.y = 0

print "%f %f" %(p1.x, p2.y)
koch(n, p1, p2)
print "%f %f" %(p1.x, p2.y)

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s.x = (2*a[0]* + b[0])/3
  s.y = (2*a[1]* + b[1])/3
  t.x = (a[0]* + 2*b.x)/3
  t.y = (a[1]* + 2*b.y)/3
  u.x = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  u.y = (t.x - s.x)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y

  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)

n = input()

p1.x = 0
p1.y = 0
p2.x = 100
p2.y = 0

print p1.x, p2.y
koch(n, p1, p2)
print p1.x, p2.y

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s[0] = (2*a[0]* + b[0])/3
  s[1] = (2*a[1]* + b[1])/3
  t[0] = (a[0]* + 2*b.x)/3
  t[1] = (a[1]* + 2*b.y)/3
  u[0] = (t[0] - s[0])*cos(pi/3) - (t[1] - s[1])*sin(pi/3) + s[0]
  u[1] = (t[0] - s[0])*sin(pi/3) + (t[1] - s[1])*cos(pi/3) + s[1]

  koch(n-1, a, s)
  print  s[0], s[1]
  koch(n-1, s, u)
  print  u[0], u[1]
  koch(n-1, u, t)
  print  t[0], t[1]
  koch(n-1, t, b)

n = input()

p1[0] = 0
p1[1] = 0
p2[0] = 100
p2[1] = 0

print p1[0], p2[1]
koch(n, p1, p2)
print p1[0], p2[1]

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s[0] = (2*a[0]* + b[0])/3
  s[1] = (2*a[1]* + b[1])/3
  t[0] = (a[0]* + 2*b[0])/3
  t[1] = (a[1]* + 2*b[1])/3
  u[0] = (t[0] - s[0])*cos(pi/3) - (t[1] - s[1])*sin(pi/3) + s[0]
  u[1] = (t[0] - s[0])*sin(pi/3) + (t[1] - s[1])*cos(pi/3) + s[1]

  koch(n-1, a, s)
  print  s[0], s[1]
  koch(n-1, s, u)
  print  u[0], u[1]
  koch(n-1, u, t)
  print  t[0], t[1]
  koch(n-1, t, b)

n = input()

p1[0] = 0
p1[1] = 0
p2[0] = 100
p2[1] = 0

print p1[0], p2[1]
koch(n, p1, p2)
print p1[0], p2[1]

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s[0] = (2*a[0] + b[0])/3
  s[1] = (2*a[1] + b[1])/3
  t[0] = (a[0] + 2*b[0])/3
  t[1] = (a[1] + 2*b[1])/3
  u[0] = (t[0] - s[0])*cos(pi/3) - (t[1] - s[1])*sin(pi/3) + s[0]
  u[1] = (t[0] - s[0])*sin(pi/3) + (t[1] - s[1])*cos(pi/3) + s[1]

  koch(n-1, a, s)
  print  s[0], s[1]
  koch(n-1, s, u)
  print  u[0], u[1]
  koch(n-1, u, t)
  print  t[0], t[1]
  koch(n-1, t, b)

n = input()

p1[0] = 0.0
p1[1] = 0.0
p2[0] = 100.0
p2[1] = 0.0

print p1[0], p1[1]
koch(n, p1, p2)
print p2[0], p2[1]

Submission status: Runtime Error

Solution code:
def koch(n, a, b):
  if n == 0:
    return
  s[0] = (2*a[0] + b[0])/3
  s[1] = (2*a[1] + b[1])/3
  t[0] = (a[0] + 2*b[0])/3
  t[1] = (a[1] + 2*b[1])/3
  u[0] = (t[0] - s[0])*cos(pi/3) - (t[1] - s[1])*sin(pi/3) + s[0]
  u[1] = (t[0] - s[0])*sin(pi/3) + (t[1] - s[1])*cos(pi/3) + s[1]

  koch(n-1, a, s)
  print s[0], s[1]
  koch(n-1, s, u)
  print u[0], u[1]
  koch(n-1, u, t)
  print t[0], t[1]
  koch(n-1, t, b)

n = input()

p1[0] = 0.0
p1[1] = 0.0
p2[0] = 100.0
p2[1] = 0.0

print p1[0], p1[1]
koch(n, p1, p2)
print p2[0], p2[1]

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y

def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2*a[0] + b[0])/3, (2*a[1] + b[1])/3)
  t = coordinates((a[0] + 2*b[0])/3, (a[1] + 2*b[1])/3)
  ux = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  uy = (t.x - s.y)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y
 
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
 
n = input()
 
p1 = point(0.0, 0.0)
p2 = point(100.0, 0.0)
 
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y

def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2*a.x + b.x)/3, (2*a.y + b.y)/3)
  t = coordinates((a.x + 2*b.x)/3, (a.y + 2*b.y)/3)
  ux = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  uy = (t.x - s.y)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y
 
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
 
n = input()
 
p1 = point(0.0, 0.0)
p2 = point(100.0, 0.0)
 
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y

def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2*a.x + b.x)/3, (2*a.y + b.y)/3)
  t = coordinates((a.x + 2*b.x)/3, (a.y + 2*b.y)/3)
  ux = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  uy = (t.x - s.y)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y
  u = point(ux, uy)
 
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
 
n = input()
 
p1 = point(0.0, 0.0)
p2 = point(100.0, 0.0)
 
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y

def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2*a.x + b.x)/3, (2*a.y + b.y)/3)
  t = coordinates((a.x + 2*b.x)/3, (a.y + 2*b.y)/3)
  ux = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  uy = (t.x - s.y)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y
  u = coordinates(ux, uy)
 
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
 
n = input()
 
p1 = coordinates(0.0, 0.0)
p2 = coordinates(100.0, 0.0)
 
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y
 
def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2*a.x + b.x)/3, (2*a.y + b.y)/3)
  t = coordinates((a.x + 2*b.x)/3, (a.y + 2*b.y)/3)
  ux = (t.x - s.x)*cos(pi/3) - (t.y - s.y)*sin(pi/3) + s.x
  uy = (t.x - s.x)*sin(pi/3) + (t.y - s.y)*cos(pi/3) + s.y
  u = coordinates(ux, uy)
  
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
  
n = input()
  
p1 = coordinates(0.0, 0.0)
p2 = coordinates(100.0, 0.0)
  
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y
 
def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2.0*a.x + b.x)/3.0, (2.0*a.y + b.y)/3.0)
  t = coordinates((a.x + 2.0*b.x)/3.0, (a.y + 2.0*b.y)/3.0)
  ux = (t.x - s.x)*cos(pi/3.0) - (t.y - s.y)*sin(pi/3.0) + s.x
  uy = (t.x - s.x)*sin(pi/3.0) + (t.y - s.y)*cos(pi/3.0) + s.y
  u = coordinates(ux, uy)
  
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
  
n = input()
  
p1 = coordinates(0.0, 0.0)
p2 = coordinates(100.0, 0.0)
  
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Submission status: Runtime Error

Solution code:
import math

class Point:
    def __init__(self, num_x, num_y):
        self.x = num_x
        self.y = num_y

def kock(n, p1, p2):
    if n == 0:
        return
    
    s = Point((2 * p1.x + 1 * p2.x) / 3.0, (2 * p1.y + 1 * p2.y) / 3.0)
    t = Point((1 * p1.x + 2 * p2.x) / 3.0, (1 * p1.y + 2 * p2.y) / 3.0)
    u = Point(0, 0)
    u.x = (t.x - s.x) * math.cos(math.pi / 3) - (t.y - s.y) * math.cos(math.pi / 3) + s.x
    u.y = (t.x - s.x) * math.sin(math.pi / 3) - (t.y - s.y) * math.cos(math.pi / 3) + s.y

    kock(n - 1, p1, s)
    print "%f %f" % (s.x, s.y)
    kock(n - 1, s, u)
    print "%f %f" % (u.x, u.y)
    kock(n - 1, u, t)
    print "%f %f" % (t.x, t.y)
    kock(n - 1, t, p2)
    
n = input()

p1 = Point(0, 0)
p2 = Point(100, 0)

print "%f %f" % (p1.x, p1.y)
kock(n, p1, p2)
print "%f %f" % (p2.x, p2.y

Submission status: Runtime Error

Solution code:
import math

n = input()

px = 0
py = 0
qx = 100
qy = 0

def koch(n, px, py, qx, qy):
 if n == 0:
  return
 
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(60) - (t.y-s.y)*math.sin(60) + s.x
 u.y = (t.x-s.x)*math.sin(60) - (t.y-s.y)*math.cos(60) + s.y

 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, s.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)

print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math

n = input()

px = 0
py = 0
qx = 100
qy = 0

def koch(n, px, py, qx, qy):
 if n == 0:
  return
 
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(pi/3) - (t.y-s.y)*math.sin(pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(pi/3) - (t.y-s.y)*math.cos(pi/3) + s.y

 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, s.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)

print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
 
n = input()
 
px = 0
py = 0
qx = 100
qy = 0
 
def koch(n, px, py, qx, qy):
 if n == 0:
  return
  
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*cos(pi/3) - (t.y-s.y)*sin(pi/3) + s.x
 u.y = (t.x-s.x)*sin(pi/3) - (t.y-s.y)*cos(pi/3) + s.y
 
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, s.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)
 
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
  
n = input()
  
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
  
def koch(n, px, py, qx, qy):
 if n == 0:
  return
   
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*cos(pi/3) - (t.y-s.y)*sin(pi/3) + s.x
 u.y = (t.x-s.x)*sin(pi/3) - (t.y-s.y)*cos(pi/3) + s.y
  
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, s.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)
  
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
   
n = input()
   
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
   
def koch(n, px, py, qx, qy):
 if n == 0:
  return
    
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(pi/3) - (t.y-s.y)*math.sin(pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(pi/3) + (t.y-s.y)*math.cos(pi/3) + s.y
   
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, s.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)
   
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
   
n = input()
   
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
   
def koch(n, px, py, qx, qy):
 if n == 0:
  return
    
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*cos(pi/3) - (t.y-s.y)*sin(pi/3) + s.x
 u.y = (t.x-s.x)*sin(pi/3) + (t.y-s.y)*cos(pi/3) + s.y
   
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.u, qx, qy)
   
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
    
n = input()
    
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
    
def koch(n, px, py, qx, qy):
 if n == 0:
  return
     
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*cos(pi/3) - (t.y-s.y)*sin(pi/3) + s.x
 u.y = (t.x-s.x)*sin(pi/3) + (t.y-s.y)*cos(pi/3) + s.y
    
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, qx, qy)
    
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
     
n = input()
     
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
     
def koch(n, px, py, qx, qy):
 if n == 0:
  return
      
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(pi/3) - (t.y-s.y)*math.sin(pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(pi/3) + (t.y-s.y)*math.cos(pi/3) + s.y
     
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, qx, qy)
     
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
      
n = input()
      
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
      
def koch(n, px, py, qx, qy):
 if n == 0:
  return
       
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
      
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, qx, qy)
      
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
       
def koch(n, px, py, qx, qy):
 if n == 0:
  return
        
 s.x = (px*2+qx)/3
 s.y = (py*2+qy)/3
 t.x = (px+qx*2)/3
 t.y = (py+qy*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
       
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, qx, qy)

n = input()
       
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
       
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
        
def koch(n, p.x, p.y, q.x, q.y):
 if n == 0:
  return
         
 s.x = (p.x*2+q.x)/3
 s.y = (p.y*2+q.y)/3
 t.x = (p.x+q.x*2)/3
 t.y = (p.y+q.y*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
        
 koch(n-1, p.x, p.y, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, q.x, q.y)
 
n = input()
        
p.x = 0.0
p.y = 0.0
q.x = 100.0
q.y = 0.0
        
print p.x, p.y
koch(n, p.x, p.y, q.x, q.y)
print q.x, q.y

Submission status: Runtime Error

Solution code:
import math
        
def koch(n, h, i, j, k):
 if n == 0:
  return
         
 s.x = (h*2+j)/3
 s.y = (i*2+k)/3
 t.x = (h+j*2)/3
 t.y = (i+k*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
        
 koch(n-1, h, i, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, j, k)
 
n = input()
        
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
        
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Submission status: Runtime Error

Solution code:
import math
       
n = input()
       
p.x = float(0)
p.y = float(0)
q.x = float(100)
q.y = float(0)
       
def koch(n, p.x, p.y, q.x, q.y):
 if n == 0:
  return
        
 s.x = (p.x*2+q.x*1)/3
 s.y = (p.y*2+q.y*1)/3
 t.x = (p.x*1+q.x*2)/3
 t.y = (p.y*1+q.y*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
       
 koch(n-1, p.x, p.y, s.x, s.y)
 print  s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print  u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print  t.x, t.y
 koch(n-1, t.x, t.y, q.x, q.y)
       
print  p.x, p.y
koch(n, p.x, p.y, q.x, q.y)
print  q.x, q.y

Submission status: Runtime Error

Solution code:
import math
         
def koch(n, h, i, j, k):
 if n == 0:
  return
          
 s.x = (h*2+j)/3
 s.y = (i*2+k)/3
 t.x = (h+j*2)/3
 t.y = (i+k*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
         
 koch(n-1, h, i, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, j, k)
  
n = input()
         
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
         
print px, py
koch(n, px, py, qx, qy)
print qx, qy