1

Тема: Допиши код

Гра з дуже простими правилами: кожен учасник бере код з попереднього повідомлення, змінює/дописує/видаляє в ньому не менше одного й не більше трьох рядків і публікує в своєму повідомленні. Код являє собою програму, яку можна запустити, при цьому вона повинна виводити хоч щось і не робити нічого шкідливого.

Отже, почали:

#!python
from math import *

ratio=.59
R=A=B=20.
def isblack (x, y):
    x=x*ratio
    rho=((x-A)**2+(y-B)**2)**.5
    phi=atan2(y-B, x-A)
    return (R/5<rho<R) and (phi%(pi/4)<pi/8)!=(rho%(2*R/5)<R/5)
for y in range(0,40):
    ln=''
    for x in range(0,79):
        ln+='%' if isblack(x,y) else ' '
    print (ln)
Прихований текст

Цю програмку я написав, коли ще тільки освоювався з python'ом. Сама по собі вона не робить нічого шкідливого, але перетворює вікно термінала на мішень.

Подякували: ostap34PHP1

2

Re: Допиши код

#!python
from math import *
 
ratio=.59
R=A=B=20.
def isblack (x, y):
    x=x*ratio
    rho=((x-A)**2+(y-B)**2)**.5
    phi=atan2(y-B, x-A)
    return (R/5<rho<R) and (phi%(pi/4)<pi/8)!=(rho%(2*R/5)<R/5)
for y in range(0,40):
    ln=''
    for x in range(0,79):
        if (y == 20) and (x == 33): ln+='1'
        elif (y == 20) and (x == 34): ln+='0'
        else: ln+='%' if isblack(x,y) else ' '
    print (ln)
Подякували: ostap34PHP1

3

Re: Допиши код

#!python
from math import *
 
ratio=.59
R=A=B=20.
def isblack (x, y):
    x=x*ratio
    rho=((x-A)**2+(y-B)**2)**.5
    phi=atan2(y-B, x-A)
    return (R/5<rho<R) and (phi%(pi/4)<pi/8)!=(rho%(2*R/5)<R/5)
for y in range(0,40):
    ln=''
    for x in range(0,79):
        if (y == 20) and (x == 35): ln+='\b\b10'
        ln+='%' if isblack(x,y) else ' '
    print (ln)        
Подякували: ostap34PHP1

4 Востаннє редагувалося koala (16.08.2021 20:22:18)

Re: Допиши код

За умовою можна було 3 рядки змінити, а ви попсували нормальний код.
Ось вам виправлений код, тепер можна щось робити (додав крапки в центральному колі):

import math

RATIO = 0.59
RADIUS = CENTER_X = CENTER_Y = 20
SECTORS = 16
CIRCLES = 5

LABELS = {(33,20):'1',(34,20):'0'}
FRAME = '/'

TARGET_WIDTH = int(2*RADIUS/RATIO)
TARGET_HEIGHT = 2*RADIUS
HORIZONTAL_LINE = FRAME*(6+TARGET_WIDTH)

def get_character (x, y):
    if (x,y) in LABELS:
        return LABELS[(x,y)]
    x = x * RATIO - CENTER_X
    y = y - CENTER_Y
    rho = math.hypot(x, y)
    phi = math.atan2(y, x)

    in_center = rho <= RADIUS / CIRCLES
    if in_center:
        return '.'
    in_stripped_area = RADIUS / CIRCLES < rho < RADIUS
    if not in_stripped_area:
        return ' '
    in_even_sector = (phi % (2*math.pi / (SECTORS/2))) < (2*math.pi / SECTORS)
    in_even_circle = (rho % (2 * RADIUS / CIRCLES)) < (RADIUS / CIRCLES)

    return '%' if in_even_sector != in_even_circle else ' '

def main():
    print(HORIZONTAL_LINE)

    for y in range(TARGET_HEIGHT+1):
        line = ''.join( get_character(x,y) for x in range(2+TARGET_WIDTH))
        print (f'{FRAME} {line} {FRAME}')

    print(HORIZONTAL_LINE)

if __name__ == '__main__':
    main()

А взагалі треба таке в клас загортати. Гм...

5 Востаннє редагувалося Droid 77 (16.08.2021 23:28:14)

Re: Допиши код

Поцілив в дев'ятку та розфарбував молоко :)

Змінений код
import math

RATIO = 0.59
RADIUS = CENTER_X = CENTER_Y = 20
SECTORS = 16
CIRCLES = 5

LABELS = {(33,20):'1',(34,20):'0'}
FRAME = '/'

TARGET_WIDTH = int(2*RADIUS/RATIO)
TARGET_HEIGHT = 2*RADIUS
HORIZONTAL_LINE = FRAME*(6+TARGET_WIDTH)

def get_character (x, y):
    if (x,y) in LABELS:
        return LABELS[(x,y)]
    x = x * RATIO - CENTER_X
    y = y - CENTER_Y
    rho = math.hypot(x, y)
    phi = math.atan2(y, x)

    in_center = rho <= RADIUS / CIRCLES
    if in_center:
        return '.'
    in_nine = rho <= 2 * RADIUS / CIRCLES
    if in_nine:
        return '9'
    in_stripped_area = RADIUS / CIRCLES < rho < RADIUS
    if not in_stripped_area:
        return 'M'
    in_even_sector = (phi % (2*math.pi / (SECTORS/2))) < (2*math.pi / SECTORS)
    in_even_circle = (rho % (2 * RADIUS / CIRCLES)) < (RADIUS / CIRCLES)

    return '%' if in_even_sector != in_even_circle else ' '

def main():
    print(HORIZONTAL_LINE)

    for y in range(TARGET_HEIGHT+1):
        line = ''.join( get_character(x,y) for x in range(2+TARGET_WIDTH))
        print (f'{FRAME} {line} {FRAME}')

    print(HORIZONTAL_LINE)

if __name__ == '__main__':
    main()

6

Re: Допиши код

Абсурдно правильний(?) код з поділом математики і екрану, підказками типів і т.п.

import math
from enum import Enum
from typing import List, Dict, Tuple

WIDTH   = 67
HEIGHT  = 40
SECTORS = 16
CIRCLES = 5

LABELS = {(33,20):'1',(34,20):'0'}

class FieldType(Enum):
    BLANK  = 1
    RED    = 2
    WHITE  = 3
    NINE   = 4
    CENTER = 5
    FRAME  = 6

CHARACTERS = {FieldType.BLANK:  ' ',
              FieldType.RED:    '%',
              FieldType.WHITE:  ' ',
              FieldType.NINE:   '9',
              FieldType.CENTER: '.',
              FieldType.FRAME:  '/'}

class Target:
    def __init__(self, *,
                 radius  : float,
                 circles : int = CIRCLES,
                 sectors : int = SECTORS):
        self.radius  : float = radius
        self.circles : int   = circles
        self.sectors : int   = sectors
        self.ring    : float = self.radius/self.circles

    def get_type(self, x: float, y: float) -> FieldType:
        rho : float = math.hypot(x, y)
        phi : float = math.atan2(y, x)

        in_center : bool = rho <= self.ring
        if in_center:
            return FieldType.CENTER
        in_nine : bool = rho <= 2 * self.ring
        if in_nine:
            return FieldType.NINE
        in_stripped_area : bool = rho <= self.radius
        if not in_stripped_area:
            return FieldType.BLANK
        in_even_sector : bool = (phi % (2*math.pi / (self.sectors/2))) < (2*math.pi / self.sectors)
        in_even_circle : bool = (rho % (2 * self.ring)) < (self.ring)
        if in_even_sector == in_even_circle:
            return FieldType.WHITE
        else:
            return FieldType.RED

class ConsoleTarget:
    def __init__(self, *,
                 width      : int                       = WIDTH,
                 height     : int                       = HEIGHT,
                 labels     : Dict[Tuple[int,int], str] = LABELS,
                 characters : Dict[FieldType, str]      = CHARACTERS):
        self.target     : Target = Target(radius = height//2)
        self.ratio      : float  = height / width
        self.center_x   : int    = int(self.ratio * width/2)
        self.center_y   : int    = height//2
        self.labels     : Dict[Tuple[int,int], str] = labels
        self.characters : Dict[FieldType, str]      = characters
        self.width      : int = int(2*self.target.radius / self.ratio)
        self.height     : int = height
        self.cframe     : str = self.characters[FieldType.FRAME]
        self.cblank     : str = self.characters[FieldType.BLANK]
        self.hline      : str = self.cframe * (6 + self.width)

    def get_character(self, x_screen: int, y_screen: int) -> str:
        if (x_screen,y_screen) in self.labels:
            return self.labels[(x_screen,y_screen)]
        x: float = x_screen * self.ratio - self.center_x
        y: float = y_screen - self.center_y
        return self.characters[self.target.get_type(x,y)]

    def draw(self):
        print(self.hline)
        for y in range(self.height+1):
            line: str = ''.join( self.get_character(x,y) for x in range(2+self.width))
            print(f'{self.cframe}{self.cblank}{line}{self.cblank}{self.cframe}')
        print(self.hline)

def main():
    c = ConsoleTarget()
    c.draw()

if __name__ == '__main__':
    main()

7 Востаннє редагувалося mamkin haker (17.08.2021 21:18:09)

Re: Допиши код

photo

https://replace.org.ua/uploads/images/11470/234a830ec5809a01b0dad5e3a3cfd103.png

code
import math
import enum
from typing import Dict, Tuple

WIDTH   = 67
HEIGHT  = 40
SECTORS = 16
CIRCLES = 5

LABELS = {(33,20) : '1',
          (34,20) : '0'}

class FieldType(enum.Enum):
    BLANK  = 1
    RED    = 2
    WHITE  = 3
    NINE   = 4
    CENTER = 5
    FRAME  = 6

CHARACTERS = {FieldType.BLANK  : ' ',
              FieldType.RED    : '%',
              FieldType.WHITE  : '.',
              FieldType.NINE   : '8',
              FieldType.CENTER : '9',
              FieldType.FRAME  : '/'}

class Target:

    def __init__(
        self, *,
        radius       : float,
        circles      : int   = CIRCLES,
        sectors      : int   = SECTORS):
    
        self.radius  : float = radius
        self.circles : int   = circles
        self.sectors : int   = sectors
        self.ring    : float = self.radius/self.circles


    def get_type(self, x: float, y: float) -> FieldType:

        rho : float = math.hypot(x, y)
        phi : float = math.atan2(y, x)

        out_center : bool = rho <= self.ring
        in_center  : bool = rho >= self.ring - 1

        if out_center and in_center:
            return FieldType.CENTER
        elif out_center:
            return FieldType.BLANK

        in_nine  : bool = 2 * self.ring >= rho >= 2 * self.ring - 1

        if in_nine:
            return FieldType.NINE
        elif rho <= 2 * self.ring + 1: #+1 щоб красиво було :3 (між 8 і % було 1 пусте кільце)
            return FieldType.BLANK

        in_stripped_area : bool = rho <= self.radius

        if not in_stripped_area:
            return FieldType.BLANK

        in_even_sector : bool = (phi % (2*math.pi / (self.sectors/2))) < (2*math.pi / self.sectors)
        in_even_circle : bool = (rho % (2 * self.ring)) < (self.ring)

        if in_even_sector == in_even_circle:
            return FieldType.WHITE
        else:
            return FieldType.RED

class ConsoleTarget:

    def __init__(
        self, *,
        width           : int                       = WIDTH,
        height          : int                       = HEIGHT,
        labels          : Dict[Tuple[int,int], str] = LABELS,
        characters      : Dict[FieldType, str]      = CHARACTERS):

        self.target     : Target                    = Target(radius = height//2)
        self.ratio      : float                     = height / width
        self.center_x   : int                       = int(self.ratio * width/2)
        self.center_y   : int                       = height//2
        self.labels     : Dict[Tuple[int,int], str] = labels
        self.characters : Dict[FieldType, str]      = characters
        self.width      : int                       = int(2*self.target.radius / self.ratio)
        self.height     : int                       = height
        self.cframe     : str                       = self.characters[FieldType.FRAME]
        self.cblank     : str                       = self.characters[FieldType.BLANK] * 2
        self.hline      : str                       = self.cframe * (7 + self.width)


    def get_character(self, x_screen: int, y_screen: int) -> str:
        if (x_screen,y_screen) in self.labels:
            return self.labels[(x_screen,y_screen)]

        x: float = x_screen * self.ratio - self.center_x
        y: float = y_screen - self.center_y

        return self.characters[self.target.get_type(x,y)]


    def draw(self) -> None:
        print(self.hline)

        for y in range(self.height+1):
            line: str = ''.join( self.get_character(x,y) for x in range(1+self.width))
            print(f'{self.cframe}{self.cblank}{line}{self.cblank}{self.cframe}')

        print(self.hline)


def main() -> None:
    c = ConsoleTarget()
    c.draw()


if __name__ == '__main__':
    main()

8

Re: Допиши код

Вони роздули мій код! *WALL*  Не пробацю, не простю! :D

import math
import enum
from typing import Dict, Tuple

WIDTH   = 67
HEIGHT  = 40
SECTORS = 17
CIRCLES = 5

LABELS = {(33,20) : '1',
          (34,20) : '0'}

class FieldType(enum.Enum):
    BLANK  = 1
    RED    = 2
    WHITE  = 3
    NINE   = 4
    CENTER = 5
    FRAME  = 6

CHARACTERS = {FieldType.BLANK  : ' ',
              FieldType.RED    : '%',
              FieldType.WHITE  : '.',
              FieldType.NINE   : '8',
              FieldType.CENTER : '9',
              FieldType.FRAME  : '/'}

class Target:

    def __init__(
        self, *,
        radius       : float,
        circles      : int   = CIRCLES,
        sectors      : int   = SECTORS):
    
        self.radius  : float = radius
        self.circles : int   = circles
        self.sectors : int   = sectors
        self.ring    : float = self.radius/self.circles


    def get_type(self, x: float, y: float) -> FieldType:

        rho : float = math.hypot(x, y)
        phi : float = math.atan2(y, x)
        rho += phi/2

        out_center : bool = rho <= self.ring
        in_center  : bool = rho >= self.ring - 1

        if out_center and in_center:
            return FieldType.CENTER
        elif out_center:
            return FieldType.BLANK

        in_nine  : bool = 2 * self.ring >= rho >= 2 * self.ring - 1

        if in_nine:
            return FieldType.NINE
        elif rho <= 2 * self.ring + 1: #+1 щоб красиво було :3 (між 8 і % було 1 пусте кільце)
            return FieldType.BLANK

        in_stripped_area : bool = rho <= self.radius

        if not in_stripped_area:
            return FieldType.BLANK

        in_even_sector : bool = (phi % (2*math.pi / (self.sectors/2))) < (2*math.pi / self.sectors)
        in_even_circle : bool = (rho % (2 * self.ring)) < (self.ring)

        if in_even_sector == in_even_circle:
            return FieldType.WHITE
        else:
            return FieldType.RED

class ConsoleTarget:

    def __init__(
        self, *,
        width           : int                       = WIDTH,
        height          : int                       = HEIGHT,
        labels          : Dict[Tuple[int,int], str] = LABELS,
        characters      : Dict[FieldType, str]      = CHARACTERS):

        self.target     : Target                    = Target(radius = height//2)
        self.ratio      : float                     = height / width
        self.center_x   : int                       = int(self.ratio * width/2)
        self.center_y   : int                       = height//2
        self.labels     : Dict[Tuple[int,int], str] = labels
        self.characters : Dict[FieldType, str]      = characters
        self.width      : int                       = int(2*self.target.radius / self.ratio)
        self.height     : int                       = height
        self.cframe     : str                       = self.characters[FieldType.FRAME]
        self.cblank     : str                       = self.characters[FieldType.BLANK] * 2
        self.hline      : str                       = self.cframe * (7 + self.width)


    def get_character(self, x_screen: int, y_screen: int) -> str:
        if (x_screen,y_screen) in self.labels:
            return self.labels[(x_screen,y_screen)]

        x: float = x_screen * self.ratio - self.center_x
        y: float = y_screen - self.center_y

        return self.characters[self.target.get_type(x,y)]


    def draw(self) -> None:
        print(self.hline)

        for y in range(self.height+1):
            line: str = ''.join( self.get_character(x,y) for x in range(1+self.width))
            print(f'{self.cframe}{self.cblank}{line}{self.cblank}{self.cframe}')

        print(self.hline)


def main() -> None:
    c = ConsoleTarget()
    c.draw()


if __name__ == '__main__':
    main()

9

Re: Допиши код

Самі ж вигадали умову: один рядок прибери, три додай. От воно і роздулося :o
Таки зафарбую молоко  :)

Новий реліз
import math
import enum
from typing import Dict, Tuple

WIDTH   = 67
HEIGHT  = 40
SECTORS = 17
CIRCLES = 5

LABELS = {(33,20) : '1',
          (34,20) : '0'}

class FieldType(enum.Enum):
    BLANK  = 1
    RED    = 2
    WHITE  = 3
    NINE   = 4
    CENTER = 5
    FRAME  = 6
    MILK   = 7

CHARACTERS = {FieldType.BLANK  : ' ',
              FieldType.RED    : '%',
              FieldType.WHITE  : '.',
              FieldType.NINE   : '8',
              FieldType.CENTER : '9',
              FieldType.FRAME  : '/',
              FieldType.MILK   : 'O'}

class Target:

    def __init__(
        self, *,
        radius       : float,
        circles      : int   = CIRCLES,
        sectors      : int   = SECTORS):
    
        self.radius  : float = radius
        self.circles : int   = circles
        self.sectors : int   = sectors
        self.ring    : float = self.radius/self.circles


    def get_type(self, x: float, y: float) -> FieldType:

        rho : float = math.hypot(x, y)
        phi : float = math.atan2(y, x)
        rho += phi/2

        out_center : bool = rho <= self.ring
        in_center  : bool = rho >= self.ring - 1

        if out_center and in_center:
            return FieldType.CENTER
        elif out_center:
            return FieldType.BLANK

        in_nine  : bool = 2 * self.ring >= rho >= 2 * self.ring - 1

        if in_nine:
            return FieldType.NINE
        elif rho <= 2 * self.ring + 1: #+1 щоб красиво було :3 (між 8 і % було 1 пусте кільце)
            return FieldType.BLANK

        in_stripped_area : bool = rho <= self.radius

        if not in_stripped_area:
            return FieldType.MILK

        in_even_sector : bool = (phi % (2*math.pi / (self.sectors/2))) < (2*math.pi / self.sectors)
        in_even_circle : bool = (rho % (2 * self.ring)) < (self.ring)

        if in_even_sector == in_even_circle:
            return FieldType.WHITE
        else:
            return FieldType.RED

class ConsoleTarget:

    def __init__(
        self, *,
        width           : int                       = WIDTH,
        height          : int                       = HEIGHT,
        labels          : Dict[Tuple[int,int], str] = LABELS,
        characters      : Dict[FieldType, str]      = CHARACTERS):

        self.target     : Target                    = Target(radius = height//2)
        self.ratio      : float                     = height / width
        self.center_x   : int                       = int(self.ratio * width/2)
        self.center_y   : int                       = height//2
        self.labels     : Dict[Tuple[int,int], str] = labels
        self.characters : Dict[FieldType, str]      = characters
        self.width      : int                       = int(2*self.target.radius / self.ratio)
        self.height     : int                       = height
        self.cframe     : str                       = self.characters[FieldType.FRAME]
        self.cblank     : str                       = self.characters[FieldType.BLANK] * 2
        self.hline      : str                       = self.cframe * (7 + self.width)


    def get_character(self, x_screen: int, y_screen: int) -> str:
        if (x_screen,y_screen) in self.labels:
            return self.labels[(x_screen,y_screen)]

        x: float = x_screen * self.ratio - self.center_x
        y: float = y_screen - self.center_y

        return self.characters[self.target.get_type(x,y)]


    def draw(self) -> None:
        print(self.hline)

        for y in range(self.height+1):
            line: str = ''.join( self.get_character(x,y) for x in range(1+self.width))
            print(f'{self.cframe}{self.cblank}{line}{self.cblank}{self.cframe}')

        print(self.hline)


def main() -> None:
    c = ConsoleTarget()
    c.draw()


if __name__ == '__main__':
    main()

10 Востаннє редагувалося mamkin haker (18.08.2021 19:45:48)

Re: Допиши код

новий реліз
import math
import enum
from typing import Dict, Tuple

WIDTH   = 67
HEIGHT  = 40
SECTORS = 17
CIRCLES = 5

LABELS = {(33,20) : '1',
          (34,20) : '0'}

class FieldType(enum.Enum):
    BLANK  = 1
    RED    = 2
    WHITE  = 3
    NINE   = 4
    CENTER = 5
    FRAME  = 6
    MILK   = 7

CHARACTERS = {FieldType.BLANK  : ' ',
              FieldType.RED    : '%',
              FieldType.WHITE  : '.',
              FieldType.NINE   : '8',
              FieldType.CENTER : '9',
              FieldType.FRAME  : '/',
              FieldType.MILK   : 'O'}

class Target:

    def __init__(
        self, *,
        radius       : float,
        circles      : int   = CIRCLES,
        sectors      : int   = SECTORS):
    
        self.radius  : float = radius
        self.circles : int   = circles
        self.sectors : int   = sectors
        self.ring    : float = self.radius/self.circles


    def get_type(self, x: float, y: float) -> FieldType:

        rho : float = math.hypot(x, y)
        phi : float = math.atan2(y, x)
        rho += phi/2

        out_center : bool = rho <= self.ring
        in_center  : bool = rho >= self.ring - 1

        if out_center and in_center:
            return FieldType.CENTER
        elif out_center:
            return FieldType.BLANK

        in_nine  : bool = 2 * self.ring >= rho >= 2 * self.ring - 1

        if in_nine:
            return FieldType.NINE
        elif rho <= 2 * self.ring + 1: #+1 щоб красиво було :3 (між 8 і % було 1 пусте кільце)
            return FieldType.BLANK

        in_stripped_area : bool = rho <= self.radius

        if not in_stripped_area:
            return FieldType.MILK

        in_even_sector : bool = (phi % (2*math.pi / (self.sectors/2))) < (2*math.pi / self.sectors)
        in_even_circle : bool = (rho % (2 * self.ring)) < (self.ring)

        if in_even_sector == in_even_circle:
            return FieldType.WHITE
        else:
            return FieldType.RED

class ConsoleTarget:

    def __init__(
        self, *,
        width           : int                       = WIDTH,
        height          : int                       = HEIGHT,
        labels          : Dict[Tuple[int,int], str] = LABELS,
        characters      : Dict[FieldType, str]      = CHARACTERS):

        self.target     : Target                    = Target(radius = height//2)
        self.ratio      : float                     = height / width
        self.center_x   : int                       = int(self.ratio * width/2)
        self.center_y   : int                       = height//2
        self.labels     : Dict[Tuple[int,int], str] = labels
        self.characters : Dict[FieldType, str]      = characters
        self.width      : int                       = int(2*self.target.radius / self.ratio)
        self.height     : int                       = height
        self.cframe     : str                       = self.characters[FieldType.FRAME]
        self.cblank     : str                       = self.characters[FieldType.BLANK] * 2
        self.hline      : str                       = self.cframe * (5 + self.width)

    def get_character(self, x_screen: int, y_screen: int) -> str:
        if (x_screen,y_screen) in self.labels:
            return self.labels[(x_screen,y_screen)]

        x: float = x_screen * self.ratio - self.center_x
        y: float = y_screen - self.center_y

        return self.characters[self.target.get_type(x,y)]


    def draw(self) -> None:
        print(self.hline)

        for y in range(self.height+2): # +1 -> +2 щоб коректно спрацював y-1
            line: str = ''.join( self.get_character(x,y-1) for x in range(1+self.width)) # y-1 щоб верхня полоса /0000/ була
            print(self.cframe + CHARACTERS[FieldType.MILK] + line + CHARACTERS[FieldType.MILK] + self.cframe) # добавив те що забув Droid 77

        print(self.hline)


def main() -> None:
    c = ConsoleTarget()
    c.draw()


if __name__ == '__main__':
    main()