Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



 1144

Need help with this coding problem please! will share the prize...help me ASAP!

by cyphur23719 - 11 March, 2022 - 09:11 PM
This post is by a banned member (cyphur23719) - Unhide
213
Posts
5
Threads
3 Years of service
#1
(This post was last modified: 11 March, 2022 - 09:12 PM by cyphur23719. Edited 1 time in total.)
You are given N number of coordinates and your task is to find out how many sides are there in the polygon.
Note: coordinates provided in the input will be in sequential form. Consider that all the coordinates make a close polygon.
 
Input
4
0 0
0 2
2 2
2 0
 
Output
4
 
Explanation:
Given N = 4,
As we can imagine, these points will make a square shape and number of sides in the polygon will be 4.

 
Input
5
0 0
0 3
3 3
3 1
3 0
 
Output
4
 
Explanation:
Given N = 5,
The coordinate (3 1) lies on the side made by coordinate (3 0) and (3 3). This will also make a square shape and have 4 sides.

Can use any programming language C/C++/Python/Java/PHP etc.
This post is by a banned member (Queen) - Unhide
This post is by a banned member (cyphur23719) - Unhide
213
Posts
5
Threads
3 Years of service
#3
(This post was last modified: 11 March, 2022 - 09:22 PM by cyphur23719. Edited 1 time in total.)
(11 March, 2022 - 09:18 PM)Queen Wrote: Show More
i have the answer bro here it is:

lol...c'mon bruh...help me out
This post is by a banned member (UberFuck) - Unhide
UberFuck  
Godlike
1.557
Posts
375
Threads
5 Years of service
#4
(This post was last modified: 12 March, 2022 - 05:31 AM by UberFuck. Edited 1 time in total.)
Code:
from __future__ import annotations

import math
from random import randint


def dumbass():
    s = [
        'Your mother hates you.',
        'God hates you.',
        'You just killed baby Jesus.',
        'Haha...no.',
        'Were you dropped on your head as a baby?',
        'A clown could do a better job than you.',
    ]
    ix = randint(0, len(s) - 1)
    print(f'\t{s[ix]} Try again dumbass.\n')


def getAngle(a, b, c):
    ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
    return ang + 360 if ang < 0 else ang


def getNumCoordinatesInput() -> int:
    while True:
        n = input('Number of coordinates: ')
        if n.isnumeric() and int(n) >= 3:
            return int(n)
        dumbass()


def getCoordinatesInput(nCoords: int):
    coords = []
    for n in range(nCoords):
        while True:
            coord = input(f'Enter coordinate # {n+1}: ')
            coord = coord.replace(',', ' ')
            if ' ' not in coord:
                dumbass()
                continue
            x, y = coord.split(' ')
            if x.isnumeric() and y.isnumeric():
                coords.append((int(x), int(y)))
                break
            dumbass()
    return coords


def getSides(coords: list) -> int | None:
    nSides = 2
    for ix, c in enumerate(coords):
        if ix < 2:
            continue
        a, b = coords[ix - 2], coords[ix - 1]
        angle = getAngle(a, b, c)
        if angle not in (0, 180):
            nSides += 1
    if nSides == 2:
        dumbass()
        return None
    return nSides


def main():
    nSides = None
    while nSides is None:
        n = getNumCoordinatesInput()
        coords = getCoordinatesInput(n)
        nSides = getSides(coords)
    print(f'\nYour shape has {nSides} sides.  Congratulations.  Now go drink a beer.')


if __name__ == '__main__':
    main()

[Image: 01-funny-gif-304-lazy-dog.gif]

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)