Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - AnonymousCelt

Pages: [1]
1
Well tell you what you should really read about the numbers before coding it out. Bruteforcing isn't a good practice. You should carefully observe the stuff.

I will give you a simple hint with which you can improve your code.

Lets examine the Fibo series :-

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, .....


Do you observe something ?

What we observe is that every third number is even.

Well that's your hint for now.


Ah, I get it.
For loop incrementing the fib sequence by 3 at a time, adding to the tally/total avoiding the need to verify whether it is even or not.
I posted this before receiving your feedback in the first project euler question. I'll look back into it all for a while before doing any more as I clearly have more to learn before I can benefit fully from the exercises.

2
Not wrong perse but why do you use a while and not a for loop ?
To be honest, because I was causing iteration within the loop and the variable was already defined I thought it would be best using the while loop as it only needs the check added.
Might be different in Python though (came from Java).

3
Looks good, yes. Did it work?
Yep, thanks for the pointer.



Think Logically and a bit mathematically as well.

How many multiple of 3 are present within 1000 --> int(999 / 3) = 333 i.e {3, 6, ... 15, ... 30, ... 999) 
How many multiple of 5 are present within 1000 --> int(995 / 5) = 199 i.e. {5, 10, ... 15... 25, 30, ... 995}

Now subtract the number of common multiples that is 15. So, how many multiples of 15 are present ? ---> 66.

Now all of these are in A.P series. So what you can do is calculate the sum of these AP series and get the result.

The Formula for Sum of A.P. Series (3 variations are there but I will use a only one)

Sum_N_Terms = N * (A + L) / 2

where N = No of terms, A = First Term, L = Last term

Therefore the complete workout would be :-

{333 * (3 + 999) / 2} + {199 * (5 + 995) / 2} - {66  * (15 + 990) / 2} = 233168

Pretty Easy :)

Now how is it an improvisation ?

The Bruteforce way would be to loop from 1 - 999 and check for multiplicity of 3 and 5 and then add. So the loop run for 999 times. But in the second case its just -> 333 + 199 + 66 = 598

So 401 loop cycles are saved.

Before solving any Project Euler problem always try to keep a mathematical mind and it will help a lot :)


EDIT:-

Python Code. (Here we use python builtin sum function)

Code: [Select]
sum(i for i in range(3, 1000, 3)) + sum(i for i in range(5, 996, 5)) - sum(i for i in range(15, 991, 15))
Will keep that in mind for future, thank you very much.

4

Task is to return the sum of all even fibonacci numbers below 4000000.

Code: [Select]

def ProblemTwo():

last = 1
current = 1
temp = 2
tally = 0

while current < 4000000:
temp = last + current
last = current
current = temp
if current % 2 == 0:
tally += current

print "My Answer is: %d" % tally




def main():
ProblemTwo()


if __name__ == '__main__':
main()


Any feedback is greatly appreciated.

5
Just calculate all multiples below 1000 instead of checking all numbers if they are multiples.


Thank you very much for the feedback, do you mean like this?


Code: [Select]

def ProblemOne():

i = 0
tally = 0
increments = 0

for i in range(0,1000,3):
if i % 5 != 0:
tally += i

for i in range(0,1000,5):
tally += i;

print "My first answer is: %d" % tally




def main():
ProblemOne()


if __name__ == '__main__':
main()


I used the two loops to ensure it doesn't add some numbers twice (such as 15) without needing to use any more memory.

6

The task:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Code: [Select]

def ProblemOne():

i = 0
tally = 0

for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
tally += i

print "My first answer is: %d" % tally




def main():
ProblemOne()


if __name__ == '__main__':
main()


Until I realised how much would be hard coded I[size=78%] considered using the following pattern:[/size]
1,3,5,6,9,10,12,15,18,20,21,24,25,27,30
Skipping by 30 each time but adding 15*i as well as 226.

7
Beginner's Corner / [Source] Tic Tac Toe, now with computer opponent
« on: March 22, 2015, 12:58:01 am »
I have changed my Tic Tac Toe to include the computer playing against you:
Code: [Select]



#String for X wins
xWin = "\t _______\n\t/       \\\n\t|VICTORY|\n\t|  FOR  |\n\t|PLAYER |\n\t|   O   |\n\t\\_______/"


#String for Y wins
yWin = "\t _______\n\t/       \\\n\t|VICTORY|\n\t|  FOR  |\n\t|PLAYER |\n\t|   Y   |\n\t\\_______/"


#String for draw
draw = "\t _______\n\t/       \\\n\t|  DRAW |\n\t\\_______/"


#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]


#Check if bot can win from a row
def BotWinRow():
    for i in range(3):
        if matrice[0][i] == matrice[1][i]:
            if matrice[0][i] == 'X' and matrice[2][i] == '-':
                matrice[2][i] = 'X'
                return False
        if matrice[0][i] == matrice[2][i]:
            if matrice[0][i] == 'X' and matrice[1][i] == '-':
                matrice[1][i] = 'X'
                return False
        if matrice[1][i] == matrice[2][i]:
            if matrice[1][i] == 'X' and matrice[o][i] == '-':
                matrice[0][i] = 'X'
                return False
    return True
           
#Check if bot can win from a collumn
def BotWinColumn():
    for i in range(3):
        if matrice[i][0] == matrice[i][1]:
            if matrice[i][0] == 'X' and matrice[i][2] == '-':
                matrice[i][2] = 'X'
                return False
        if matrice[i][0] == matrice[i][2]:
            if matrice[i][0] == 'X' and matrice[i][1] == '-':
                matrice[i][1] = 'X'
                return False
        if matrice[i][1] == matrice[i][2]:
            if matrice[i][1] == 'X' and matrice[i][0] == '-':
                matrice[i][0] = 'X'
                return False
    return True


#Check if bot can win from a diagonal
def BotWinDiagonal():


    #Top Left to Bottom Right
    if matrice[0][0] == matrice[1][1]:
        if matrice[0][0] == 'X' and matrice[2][2] == '-':
            matrice[2][2] = 'X'
            return False
    if matrice[0][0] == matrice[2][2]:
        if matrice[0][0] == 'X' and matrice[1][1] == '-':
            matrice[1][1] = 'X'
            return False
    if matrice[1][1] == matrice[2][2]:
        if matrice[1][1] == 'X' and matrice[0][0] == '-':
            matrice[0][0] = 'X'
            return False


    #Top Right to Bottom Left
    if matrice[2][0] == matrice[1][1]:
        if matrice[2][0] == 'X' and matrice[0][2] == '-':
            matrice[0][2] = 'X'
            return False
    if matrice[2][0] == matrice[0][2]:
        if matrice[2][0] == 'X' and matrice[1][1] == '-':
            matrice[1][1] = 'X'
            return False
    if matrice[1][1] == matrice[0][2]:
        if matrice[1][1] == 'X' and matrice[2][0] == '-':
            matrice[2][0] = 'X'
            return False


    return True




#Check if opponent can win from a row
def OpponentWinRow():
    for i in range(3):
        if matrice[0][i] == matrice[1][i]:
            if matrice[0][i] == 'O' and matrice[2][i] == '-':
                matrice[2][i] = 'X'
                return False
        if matrice[0][i] == matrice[2][i]:
            if matrice[0][i] == 'O' and matrice[1][i] == '-':
                matrice[1][i] = 'X'
                return False
        if matrice[1][i] == matrice[2][i]:
            if matrice[1][i] == 'O' and matrice[0][i] == '-':
                matrice[0][i] = 'X'
                return False
    return True




#Check if opponents can win from a collumn
def OpponentWinColumn():
    for i in range(3):
        if matrice[i][0] == matrice[i][1]:
            if matrice[i][0] == 'O' and matrice[i][2] == '-':
                matrice[i][2] = 'X'
                return False
        if matrice[i][0] == matrice[i][2]:
            if matrice[i][0] == 'O' and matrice[i][1] == '-':
                matrice[i][1] = 'X'
                return False
        if matrice[i][1] == matrice[i][2]:
            if matrice[i][1] == 'O' and matrice[i][0] == '-':
                matrice[i][0] = 'X'
                return False
    return True




#Check if opponents can win from a Diagonal
def OpponentWinDiagonal():


    #Top Left to Bottom Right
    if matrice[0][0] == matrice[1][1]:
        if matrice[0][0] == 'O' and matrice[2][2] == 'O':
            matrice[2][2] = 'X'
            return False
    if matrice[0][0] == matrice[2][2]:
        if matrice[0][0] == 'O' and matrice[1][1] == 'O':
            matrice[1][1] = 'X'
            return False
    if matrice[1][1] == matrice[2][2]:
        if matrice[1][1] == 'O' and matrice[0][0] == 'O':
            matrice[0][0] = 'X'
            return False


    #Top Right to Bottom Left
    if matrice[2][0] == matrice[1][1]:
        if matrice[2][0] == 'O' and matrice[0][2] == 'O':
            matrice[0][2] = 'X'
            return False
    if matrice[2][0] == matrice[0][2]:
        if matrice[2][0] == 'O' and matrice[1][1] == 'O':
            matrice[1][1] = 'X'
            return False
    if matrice[1][1] == matrice[0][2]:
        if matrice[1][1] == 'O' and matrice[2][0] == 'O':
            matrice[2][0] = 'X'
            return False
       
    return True


#Defence against human tactic
def TacticalDefense():
    #Top Left
    if matrice[0][0] == 'O' and matrice[2][2] == '-':
        matrice[2][2] = 'X'
        return False


    #Top Right
    if matrice[2][0] == 'O' and matrice[0][2] == '-':
        matrice[0][2] = 'X'
        return False


    #Bottom Right
    if matrice[2][2] == 'O' and matrice[0][0] == '-':
        matrice[0][0] = 'X'
        return False


    #Bottom Left
    if matrice[0][2] == 'O' and matrice[2][0] == '-':
        matrice[2][0] = 'X'
        return False


    return True




#Select next move
def OtherNextMove():


    #Middle
    if matrice[1][1] == '-':
        matrice[1][1] = 'X'
        return False


    #Top Left
    if matrice[0][0] == '-':
        matrice[0][0] = 'X'
        return False


    #Top Right
    if matrice[2][0] == '-':
        matrice[2][0] = 'X'
        return False


    #Bottom Right
    if matrice[2][2] == '-':
        matrice[2][2] = 'X'
        return False


    #Bottom Left
    if matrice[0][2] == '-':
        matrice[0][2] = 'X'
        return False
   
    #Up
    if matrice[1][0] == '-':
        matrice[1][0] = 'X'
        return False
   
    #Right
    if matrice[2][1] == '-':
        matrice[2][1] = 'X'
        return False


    #Down
    if matrice[1][2] == '-':
        matrice[1][2] = 'X'
        return False


    #Left
    if matrice[0][1] == '-':
        matrice[0][1] = 'X'
        return False


    return True


   
#Bot takes a turn
def BotShot():
    MyTurn = True
    if MyTurn:
        MyTurn = BotWinRow()
    if MyTurn:
        MyTurn = BotWinColumn()
    if MyTurn:
        MyTurn = BotWinDiagonal()
    if MyTurn:
        MyTurn = OpponentWinRow()
    if MyTurn:
        MyTurn = OpponentWinColumn()
    if MyTurn:
        MyTurn = OpponentWinDiagonal()
    if MyTurn:
        MyTurn = TacticalDefense()
    if MyTurn:
        MyTurn = OtherNextMove()
    if MyTurn:
        print "I dont know"
    print "Your turn."


#Print Matrice
def printMatrix():
    print "\n\n"
    for i in range(3):
        print " | ",
        for j in range(3):
            print matrice[i][j],
            print " | ",
        print "\n",
    print"\n\n"
   
#Take player one's turn
def TakeTurn():
    printMatrix()
    x = int(input("Enter X co-ordinate: ")) - 1
    y = int(input("Enter Y co-ordinate: ")) - 1
    if matrice[y][x] == '-':
        matrice[y][x] = 'O'
    else:
        print "Please choose another square\n\n"
        playerOneTurn()


#Test if Game Has Come to End
def gameStatus():
    over = '='
    for i in range(3):
        for j in range(3):
            if matrice[i][j] == '-':
                over = '-'
                break
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i]:
            victory(matrice[1][i])
        if matrice[i][0] == matrice[i][1] == matrice[i][2]:
            victory(matrice[i][1])
    if matrice[0][0] == matrice[1][1] == matrice[2][2]:
        victory(matrice[1][1])
    if matrice[2][0] == matrice[1][1] == matrice[0][2]:
        victory(matrice[1][1])
    if over == '=':
        victory(over)


#Victory Print
def victory(c):
    if c == 'O':
        print yWin
        printMatrix()
        raise SystemExit
    if c == 'X':
        print xWin
        printMatrix()
        raise SystemExit
    if c == '=':
        print draw
        printMatrix()
        raise SystemExit
   


#main logic
def main():
    while True:
        TakeTurn()
        gameStatus()
        BotShot()
        gameStatus()


#Set to run main
if __name__ == "__main__":
    main()



8
Beginner's Corner / [Python] Very Basic Text Editor
« on: March 12, 2015, 06:19:21 pm »
Code: [Select]

from Tkinter import *
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfilename


def OpenFile():
    name = askopenfilename()
    with file(name) as f:
        T.insert(END,f.read())


def SaveFileAs():
    name = asksaveasfilename()
    a = open(name, 'w')
    a.write(T.get('1.0', 'end'))


#Create Menu
root = Tk()
menu = Menu(root)
root.config(menu=menu)


#File Menu
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open", command=OpenFile)
filemenu.add_command(label="Save As", command=SaveFileAs)
           
#Create Text Box
T = Text(root,height=50, width=100)
T.pack()


#Main
root.mainloop()

9
Beginner's Corner / Re: [Python] Tic Tac Toe
« on: March 09, 2015, 09:51:21 pm »

I have updated it to protect against the wrong number being entered and changed the two player turn methods into takeTurn. I haven't done anything about the victory method yet. 

Code: [Select]

#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]


#Print Matrice
def printMatrix():
    print "\n\n"
    for i in range(3):
        print " | ",
        for j in range(3):
            print matrice[i][j],
            print " | ",
        print "\n",
    print"\n\n"
   
#Take player one's turn
def takeTurn(c):
    printMatrix()
    print "Player " ,c
    x = int(input("Enter X co-ordinate: "))
    y = int(input("Enter Y co-ordinate: "))
    if y > 3 or y < 1 or x > 3 or x < 1:
        print "Please choose another square\n\n"
        takeTurn(c)
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = c
    else:
        print "Please choose another square\n\n"
        takeTurn(c)


#Test if Game Has Come to End
def gameStatus():
    over = '='
    for i in range(3):
        for j in range(3):
            if matrice[i][j] == '-':
                over = '-'
                break
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i]:
            victory(matrice[1][i])
        if matrice[i][0] == matrice[i][1] == matrice[i][2]:
            victory(matrice[i][1])
    if matrice[0][0] == matrice[1][1] == matrice[2][2]:
        victory(matrice[1][1])
    if matrice[2][0] == matrice[1][1] == matrice[0][2]:
        victory(matrice[1][1])
    if over == '=':
        victory(over)


#Victory Print
def victory(c):
    if c == 'X':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  ONE  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == 'Y':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  TWO  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == '=':
        print "\t _______"
        print "\t/       \\"
        print "\t|  DRAW |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
   
#main logic
def main():
    while True:
        takeTurn('X')
        gameStatus()
        takeTurn('O')
        gameStatus()


#Set to run main
if __name__ == "__main__":
    main()


Thank you very much for the feedback.

10
Beginner's Corner / [Python] Tic Tac Toe
« on: March 09, 2015, 08:55:22 pm »
This is my first time programming Python so all feedback is appreciated.
Code: [Select]

#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]


#Print Matrice
def printMatrix():
    print "\n\n"
    for i in range(3):
        print " | ",
        for j in range(3):
            print matrice[i][j],
            print " | ",
        print "\n",
    print"\n\n"
   
#Take player one's turn
def playerOneTurn():
    printMatrix()
    print "Player One:"
    x = int(input("Enter X co-ordinate: "))
    y = int(input("Enter Y co-ordinate: "))
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = 'X'
    else:
        print "Please choose another square\n\n"
        playerOneTurn()


#Take player two's turn
def playerTwoTurn():
    printMatrix()
    print "Player Two:"
    x = int(input("   Enter X co-ordinate: "))
    y = int(input("   Enter Y co-ordinate: "))
    x -= 1
    y -= 1
    if matrice[y][x] == '-':
        matrice[y][x] = 'O'
    else:
        print "Please choose another square\n\n"
        playerTwoTurn()


#Test if Game Has Come to End
def gameStatus():
    over = '='
    for i in range(3):
        for j in range(3):
            if matrice[i][j] == '-':
                over = '-'
                break
    for i in range(3):
        if matrice[0][i] == matrice[1][i] == matrice[2][i]:
            victory(matrice[1][i])
        if matrice[i][0] == matrice[i][1] == matrice[i][2]:
            victory(matrice[i][1])
    if matrice[0][0] == matrice[1][1] == matrice[2][2]:
        victory(matrice[1][1])
    if matrice[2][0] == matrice[1][1] == matrice[0][2]:
        victory(matrice[1][1])
    if over == '=':
        victory(over)


#Victory Print
def victory(c):
    if c == 'X':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  ONE  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == 'Y':
        print "\t _______"
        print "\t/       \\"
        print "\t|VICTORY|"
        print "\t|  FOR  |"
        print "\t|PLAYER |"
        print "\t|  TWO  |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
    if c == '=':
        print "\t _______"
        print "\t/       \\"
        print "\t|  DRAW |"
        print "\t\\_______/"
        printMatrix()
        raise SystemExit
   
#main logic
def main():
    while True:
        playerOneTurn()
        gameStatus()
        playerTwoTurn()
        gameStatus()


#Set to run main
if __name__ == "__main__":
    main()

Pages: [1]