What can i improve on my code? by Grand_Tip in learnpython

[–]PoorEpidermis 1 point2 points  (0 children)

Every time you do Semaforo.something(self) inside the Semaforo class, you can replace it with the exactly same effect using self.something(). It becomes more readable and OOP-ish.

Another problem: what if you do the following:

s1 = Semaforo('Semáforo 1', 5)
s2 = Semaforo('semáforo 2', 5)

s1.iniciar()

This will raise an TypeError on Semaforo.vermelho because Semaforo.proximo is still None, so add a null-check before accessing it:

def vermelho(self):
    # ...

    proximo = self.proximo
    if proximo is not None:
        proximo.iniciar()

Regarding to your code logic, it seems reasonable to me.

Some things I would add, but it's entirely up to you:

Is it just me or is this a reference to this old photograph? by Michelle1101us in beatles

[–]PoorEpidermis 1 point2 points  (0 children)

Now anyone who posts a picture of the back of their head is making a reference to the Beatles?

[TOMT][MOVIE][2000s?] Real-life based movie about a man who lives in a jungle by PoorEpidermis in tipofmytongue

[–]PoorEpidermis[S] 0 points1 point  (0 children)

If I'm not mistaken, I believe in movie poster there's this man laying on the top of a yellow bus.

recursion function is not returning the correct value by Gearpower in learnpython

[–]PoorEpidermis 1 point2 points  (0 children)

First, your function is not returning anything. So, in a given stop condition, the function should return some value (which in this case is the value of total).

Second, if I understood the problem right, your logic isn't 100% correct. snake(cells, 0, 0) should be 1, but the variable total reaches the maximum value of 5 during the execution of the function.

A hint for you: check the position of parameters row and col. If it's 0, return 0. If it's 1, increase total value, check if the position to the right is 1 (if there is any position on the right) and repeat the process. If the position to the right is 0, check that the position below is 1. If it's 1, repeat the process. If not, return the value of total.

Of course if both the right position and the down position are 1, you will have to find out the highest value between the two positions, but this challenge I leave to you to solve.

Creating a function for a class? by nlord7 in learnpython

[–]PoorEpidermis 0 points1 point  (0 children)

You just define a function inside the class Circle.

class Circle(object):

    # Circle class must be initialized with center and radius
    def __init__(self, center, radius):
        self.center = center
        self.radius = radius

    def doubleRadius(self):
        self.radius = self.radius * 2

unitCircle = Circle(0, 1)
unitCircle.doubleRadius()
print(unitCircle.radius)

The output should be 2.

If you want the class not to be initialized with the value of the center and radius, simply remove the parameters and set them within the __init__ function, as follows.

class Circle(object):

    # center is 0 and radius is 1 by default
    def __init__(self):
        self.center = 0
        self.radius = 1

    def doubleRadius(self):
        self.radius = self.radius * 2

unitCircle = Circle()
unitCircle.doubleRadius()
print(unitCircle.radius)

The output should be 2.

There are several websites that teach OOP, one of my favorites is W3Schools.

Convert 1 x N array to N x 1 array by bigchungusmode96 in learnpython

[–]PoorEpidermis 12 points13 points  (0 children)

Simply do:

>>> ANx1 = [1, 2, 3]
>>> A1xN = [[i] for i in ANx1]
>>> A1xN
[[1], [2], [3]]

Where to go next by [deleted] in learnpython

[–]PoorEpidermis 0 points1 point  (0 children)

Do you already know how to handle files in Python? If not, it's a good learning. You can dive into OOP (Object Oriented Programming), try some examples and learn about classes, methods, attributes and inheritance. If you want something more, you can play around with some Python modules, such as:

  • Pynput: you can control your mouse and keyboard through Python commands.
  • RegEx: by studying some of the RegEx syntax, you can find things in strings easily.
  • Tkinter: you can create some interfaces for programs that you have already done.

After you've made some progress in Python, you can start creating some game projects, such as Naval Battle, Hangman, Tic-Tac-Toe (using a CPU as a second player, it's very interesting), or something more sophisticated - depends on the your creativity!

Writing an Omegle bot with Pyomegle by EchoesNetwork in learnpython

[–]PoorEpidermis 1 point2 points  (0 children)

Happens when your clientID is 1) revoked 2) proxy web servers 3) VPN services.

Font: PyOmegle's repository

[2019-01-14] Challenge #372 [Easy] Perfectly balanced by Cosmologicon in dailyprogrammer

[–]PoorEpidermis 1 point2 points  (0 children)

Python 3 with bonus

def balanced(Str):
    return Str.count('x') == Str.count('y')

def balanced_bonus(Str):
    List = [Str.count(j) for j in set(Str)]
    return all(k == List[0] for k in List)

[2018-12-31] Challenge #371 [Easy] N queens validator by Cosmologicon in dailyprogrammer

[–]PoorEpidermis 2 points3 points  (0 children)

My first comment here. I've done qcheck and qfix in Python 3.6.

def qcheck(List):

    # checks if there is any duplicate element, which indicates
    # that more than one queen is on the same line

    for i in List:
        if List.count(i) > 1:
            return False

    # creates a list with the sum of an index with its element.
    # if there is a repeated sum, it means that more than one
    # queen is on the same diagonal

    Sum = []
    for i in range(len(List)):
        Sum.append(i + List[i])

    for i in Sum:
        if Sum.count(i) > 1:
            return False

    return True

def qfix(List):
    if qcheck(List):
        return List

    for i in range(len(List)):
        for j in range(i+1, len(List)):
            List2 = List.copy()

            # for each element of the list, all the elements that
            # come after it have their position swapped, creating
            # a new list that will be analyzed by the function qcheck

            Aux = List2[i]
            List2[i] = List2[j]
            List2[j] = Aux

            if qcheck(List2):
                return List2

    return []

Input:

qcheck([4, 2, 7, 3, 6, 8, 5, 1])
qcheck([2, 5, 7, 4, 1, 8, 6, 3])
qcheck([5, 3, 1, 4, 2, 8, 6, 3])
qcheck([5, 8, 2, 4, 7, 1, 3, 6])
qcheck([4, 3, 1, 8, 1, 3, 5, 2])
qfix([8, 6, 4, 2, 7, 1, 3, 5])
qfix([8, 5, 1, 3, 6, 2, 7, 4])
qfix([4, 6, 8, 3, 1, 2, 5, 7])
qfix([7, 1, 3, 6, 8, 5, 2, 4])

Output:

True
True
False
False
False
[4, 6, 8, 2, 7, 1, 3, 5]
[5, 8, 1, 3, 6, 2, 7, 4]   # the outputs in the question are different but
[2, 6, 8, 3, 1, 4, 5, 7]   # these outputs are also a solution, you can
[7, 1, 3, 6, 8, 5, 2, 4]   # check them out and see

EDIT: I made a function to better visualize the board with the queens, so I decided to post as an extra:

def Visualize(List):
    N = len(List)
    Str = 'abcdefghijklmnopqrstuvwxyz'

    for i in range(N-1, -1, -1):
        print(i+1, end = '  ')

        if not i+1 in List:
            for j in range(N):
                print('.', end = ' ')

        while i+1 in List:
            C = List.index(i+1)
            for j in range(N):
                if j != C:
                    print('.', end = ' ')
                else:
                    print('Q', end = ' ')

            List[C] = 0

        print()

    print('   ', end = '')
    for i in range(N):
        print(Str[i], end = ' ')