Follow us on Twitter!
Ideas are far more powerful than guns.
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 24
Guests Online: 22
Members Online: 2

Registered Members: 82843
Newest Member: hx47
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Page 1 of 2 1 2 >
Author

Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 06-04-09 22:59
Hi again guys :P

I'd like to see some feedback/tips about how I can improve my python programing, so I thought maybe the best way to do this is by posting a couple of projects I've been working on.

Project 1:
PQ formula Calculator, ax^2+bx+c=0 using the PQ formula to solve x.
any ideas about how I can improve this program? maybe add something or alter something to have a shorter code?

Code

#Simple PQ Calculator
from math import sqrt

print 'This program use the PQ formula to solve the following equation: ax^2+bx+c=0'
print 'All you have to do is tell the program the values of a,b,and c.'
print

def pq(a,b,c):
    b=b/a
    c=c/a
    print               
    if ((b/2)**2-c)>0:
        print 'x1= ',(-b/2)+sqrt((b/2)**2-c),'  x2= ',(-b/2)-sqrt((b/2)**2-c)
        #This happens if everything is OK!
        return 'Problem Solved!'
    else:
        return 'Cant give you an answer :/'
        #This happens when ((b/2)**2-c) <= 0.
       
#Enter a,b and c
a=float(input('Insert The Value of a: '))
b=float(input('Insert The Value of b: '))
c=float(input('Insert The Value of c: '))

#Call the function pq
print pq(a,b,c)






Project 2:
This one is not finished yet, I have a small problem which I need help with :(

"You just wrote a loop allowing the input of 10 names into a list. Alas, you made an error as you entered name number 8. Redesign your input loop, so you can correct the error easily without having to retype the previous 7 names."

Code

#loop corrector
#This is a program that saves and edit info inside of a list.
listan={} #The List
for i in range(1,10):
    name=raw_input('Insert a name:  ')
    listan[i]=name
    print i,name
    print listan
    print

while 1: #Correction loop
    print
    print 'If you want to exit, type exit.'
    corrector=input('Enter the number of the string you want to correct: ')
    if corrector=='exit':
        break
    elif listan.has_key(corrector):
        listan[corrector]=raw_input('Insert the new name: ')
        print
        print corrector,name
        print listan
    else:
        print 'Unknown command. Please try again'





This problem with this function is that I use "corrector=input('..')". When using input() you get an error when entering the word exit, and using the raw_input() function results wrong key:value in the dictionary so that the loop jumps to 'Unknown command. Please try again'.

how can i fix this??

some help/feedback would be highly appreciated :)

//D.H.

Edited by Demons Halo on 06-04-09 23:04
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 06-04-09 23:22
For project one, instead of returning 'Cant give you an answer :/' if the value inside the square root is negative, you could have it give the answer in terms of complex numbers (i.e. "i").

And for project 2, you're welcome:
Code
#loop corrector
#This is a program that saves and edit info inside of a list.
listan={} #The List
for i in range(1,10):
    name=raw_input('Insert a name:  ')
    listan[i]=name
    print i,name
    print listan
    print

loop=1
while loop==1: #Correction loop
    print 'If you want to exit, type exit.'
    corrector=raw_input('Enter the number of the string you want to correct: ')
    if corrector=='exit':
        loop=0
    elif listan.has_key(int(corrector)):
        listan[int(corrector)]=raw_input('Insert the new name: ')
        print
        print corrector,name
        print listan
    else:
        print 'Unknown command. Please try again'




EDIT: typo


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 06-04-09 23:23
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 06-04-09 23:35
thnx for the fast answer Smile

@ project 1
I've not studied "higher" math Pfft the PQ formula is like the high-end of my mathematical knowledge xD

@ project 2
Wow! so it was that simple! Let me see if I understand that one correctly:
you had the corrector function inside of an int so that the only way that one gets executed is if the corrector value is an integral. cool!

thanks a lot Grin
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 06-04-09 23:56
Demons Halo wrote:thnx for the fast answer Smile

No problem.

@ project 1
I've not studied "higher" math Pfft the PQ formula is like the high-end of my mathematical knowledge xD

I must be getting old. Back when I learned it, it was called the quadratic formula. I've never heard the term PQ before.

@ project 2
Wow! so it was that simple! Let me see if I understand that one correctly:
you had the corrector function inside of an int so that the only way that one gets executed is if the corrector value is an integral. cool!

No, what the int() method does is pretty much the same as your use of the float() method in your other problem. It typecasts corrector to an integer because raw_input() takes the input as a string. The keys for your array (you're using an array, not a list) are integers. '3' is not equal to 3.

A few more corrections on your terminology just for your own knowledge:
corrector is a 'variable', not a 'function'.
int converts to an 'integer', not an 'integral'. Integral is something very different.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 07-04-09 00:17

No, what the int() method does is pretty much the same as your use of the float() method in your other problem. It typecasts corrector to an integer because raw_input() takes the input as a string. The keys for your array (you're using an array, not a list) are integers. '3' is not equal to 3.

A few more corrections on your terminology just for your own knowledge:
corrector is a 'variable', not a 'function'.
int converts to an 'integer', not an 'integral'. Integral is something very different.


my way of expressing myself sucks sometimes, I know... Pfft haha
thanks for the corrections, rereading my last post made me feel so stupid considering the terms I use :$
I've heard the word integral, so i though that's what int is haha Pfft

well now that you have explained it, it seems like I have the same problem with another project, at least now I know the solution Smile

thanks a lot dude! I really appreciate it

//D.H.
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 09-04-09 15:16
instead of creating a new topic I'll just post here :P

Code

import random

numlist=['1','2','3','4','5','6','7','8','9','0']
charlist=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
numcharlist=['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

def shuffler():
    random.shuffle(numlist)
    random.shuffle(charlist)
    random.shuffle(numcharlist)
       
while 1:
    passtype=int(input("""1)Only numbers
2)Only letters
3)Characters + letters
4)Exit
What do you want the password to consist of? Enter a number: """))
    if passtype==1:
        length=input('How long should the password be? ')
        shuffler()
        print numlist[:length]
        break
    elif passtype==2:
        length=input('How long should the password be? ')
        shuffler()
        print charlist[:length]
        break
    elif passtype==3:
        length=input('How long should the password be? ')
        shuffler()
        print numcharlist[:length]
        break
    elif passtype==4:
        print 'Bye Bye!'
        break
    else:
        print 'Please enter a number 1-4:'
        print





This is a random password generator, created so that the user can choose the length and the type of the password (num, char or num-char).

when you run the program and choose the type and the length the program shows the following:

Code

1)Only numbers
2)Only letters
3)Characters + letters
4)Exit
What do you want the password to consist of? Enter a number: 3
How long should the password be? 5
['l', 'n', 'F', 'I', 'v']





How can I get rid of the [],'' characters? I want the password to be shown as a word, not as individual characters.
and also, any feedback on the main code? it sure seems a bit too long for a password generator, can I change something to make it shorter?

Thnx in advance

//D.H.

Edited by Demons Halo on 09-04-09 15:18
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 09-04-09 16:37
Code
import random

numlist=['1','2','3','4','5','6','7','8','9','0']
charlist=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
numcharlist=numlist+charlist
def shuffler():
    random.shuffle(numlist)
    random.shuffle(charlist)
    random.shuffle(numcharlist)
       
while 1:
    passtype=int(input("""1)Only numbers
2)Only letters
3)Characters + letters
4)Exit
What do you want the password to consist of? Enter a number: """))
    out=""
    if passtype==1:
        length=input('How long should the password be? ')
        shuffler()
        print out.join(numlist[:length])
        break
    elif passtype==2:
        length=input('How long should the password be? ')
        shuffler()
        print out.join(charlist[:length])
        break
    elif passtype==3:
        length=input('How long should the password be? ')
        shuffler()
        print out.join(numcharlist[:length])
        break
    elif passtype==4:
        print 'Bye Bye!'
        break
    else:
        print 'Please enter a number 1-4:'
        print




That fixes your problem. There's not really much you can do to make it shorter. It's not a very long program.

EDIT:formatting


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 09-04-09 16:38
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 09-04-09 17:44
Slap me if im wrong, but you could have used a switch: case here could you? Much nicer than nested elseif statements.


Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 09-04-09 18:02
jjbutler88 wrote:
Slap me if im wrong, but you could have used a switch: case here could you? Much nicer than nested elseif statements.

Python doesn't have a built in switch-case. The alternative would be longer than what's already there.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 09-04-09 18:19
thnx a lot Grin

I might need help with a couple more minor syntax stuff later on, I hope you don't mind if I post them here or pm you Smile

//D.H.
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 09-04-09 23:13
I dont want to start a new thread but i was wondering if someone could help me with me python script for timed 5. I know it works it prints out the right numbers and isdoing it in time but for some reason it says it's wrong. I was wondering if someone who knows python could help me out on this.


Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 09-04-09 23:24
post your code here if it's ok with you, I might be able to check it out and learn something from it, and maybe help you out in case I can Grin

Edited by Demons Halo on 09-04-09 23:25
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 09-04-09 23:27
Demons Halo wrote:
post your code here if it's ok with you, I might be able to check it out and learn something from it, and maybe help you out in case I can Grin

Code for timed 5, no posting publicly, no spoilers.


Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 09-04-09 23:34
@fallingmidget - Go on AIM and I can take a look at it.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 11-04-09 16:55
my understanding of python have surely become a lot better, but my understanding of how to work the math still suck :P

I'm pretty close to the answer but I don't want to keep trying until I find the answer, instead I want to create a program that does that for me.
here is the problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?


I can immidietly say that the answer has the following characteristics:
- ends with a zero (dividable by 10)
- must be a multiplayer of 19 (19*a=the answer)
- must be a multiplayer of 18 (18*x=the answer) --> divided by 18 is the same as 9,6,3
- must be a multiplayer of 14 (14*y=the answer) --> divided by 14 is the same as 7
- must be a multiplayer of 11 (11*b=the answer)
etc.etc.


What's the best way to tackle this problem?

Code

x=5040 #a good place to start
while 1:
    print x
    for i in range(1,21):
        if x%i==0:
            print i,'nice'
        else:
            print i,'lol try'
    print
    print
    x=x+720 #720 is a number dividable with: 18->(9,6,3), 16->(12,8,4,2), 10->(15,5). The number is however not dividable with: 11,13,14(7),17,19





Should I create a loop that keeps adding +720 / loop until if founds the number that is dividable with all values 1-20, or is there a better mathematical way of doing this?

I thought about putting all the values (1-20) in a list and use x%list, but that seems impossible since the % statement does not take in lists :/

any ideas? :P

cheers

//D.H.
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 11-04-09 21:01
You could do it that way, but the answer is a very large number, so it wouldn't be very efficient. You should look up LCM (least common multiple) and prime factorization:
http://en.wikiped. . .torization

And just because I'm bored, here you go:
(2^2*5), 19, (3^2*2) , 17, 2^4, (7*2), 13, (2^2*3), 11, (2*5), 3^2, 2^3, 7, (2*3), 5, 2^2, 3, 2, 1

Which results in:
19 * 3^2 * 17 * 2^4 * 7 * 13 * 11 * 5 = 232,792,560

So if you really want to make a good program to find that answer, make a function that can find the prime factorization of a number.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 12-04-09 13:22
I see.
I do hate math sometimes, and the more programming you learn the more math there is involved -_-

I created a prime calc, works nice Grin

thnx a lot once again Smile

//D.H.

base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 13-04-09 12:00
Edit:
I've made a decent prime calc, but the problem now is that this calc is damn SLOW!

Code

from math import sqrt
from time import time

def primes(n):
    s=time()
    x=input('Which prime number do you want? Ex. the forth prime is number 7. ')
    dictio={1:2}
   
    min_loop=2                     
    max_loop=x+1

    while min_loop<max_loop:
        n+=2
        i=3
        while 1:           
            if n%i==0 and n==i:     
                dictio[min_loop]=i       
                min_loop+=1
                break
            elif n%i==0:
                break
            else:
                i+=1
               
    print time()-s
    print 'The',x,'prime has the value:'
    return i


print primes(1)                     





it takes ~75sec to get the 10001 prime. I'm trying to replace the while 1: with while i<=sqrt(n)+1 but the program keeps on not giving any response. It seems like I get stuck in a loop as soon as I insert the sqrt function into this code.
what should I do? :P

cheers

//D.H.

Edited by Demons Halo on 13-04-09 16:38
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Some Python Feedback plz

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 13-04-09 17:06
Take a look at this program I put in the code bank a while back: http://www.hellbo. . .ython.html

Shouldn't be too hard to take what you need from that and implement it.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: Some Python Feedback plz

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 14-04-09 23:25
Thnx a lot Smile that code helped me out Grin


Now when I started this whole programming thing it was pretty easy since all you needed to do was to think logically, describe your way of thinking with python and making the PC calculate the results. But now it feels like I've reached the level where I need to go in deeper to gain some more programming experience.


The mathematical problems I'm working on right now (like the sum of all the primes below two million) requires a bit more "outside the box thinking". earlier i would have done some easy programming but this unfortunately does not work anymore now that the takes are a lot more advance!

So I was thinking of asking you guys for some tips about how to create faster programs. the codes I write takes ages cause they use simple mathematical way of thinking. How do you guys do/think before start coding a program?

What methods are the best when working with large numbers? for instance I've tried few methods for finding prime numbers:

- Storng ex. 2 000 000 numbers in a list, and then % all the values in the list with i in range (2,200 000)
- Starting with number 3 and increasing the number with +2 for each loop, dividing it by all the numbers from 2 to 2000 000
- etc.

such methods are really basic and takes ages for the program to work out Sad

I know you guys have uploaded some great codes, but I don't want to steal your codes Pfft I want to learn how to create my own codes that runs faster than the methods mentioned above!

I'd really appreciate it in case you guys could give me some tips!!

thnx once again for your time

//D.H.

base_dropper@hotmail.com www.demonshalo.com
Page 1 of 2 1 2 >