guys ,, I have an assignment

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1156 Views - Last Post: 21 April 2013 - 01:25 AM Rate Topic: -----

#1 101newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-April 13

guys ,, I have an assignment

Posted 17 April 2013 - 02:34 AM

Dear Python developers ,

I am a newbie in python and still learning the basics but I need your help in these urgent couple of questions ,,, please be kind and help me with below ... All my appreciation to u

1- Write a software application , where the input is the time and the output is the degree between the minutes and hours hands.
the input will be in this format : "hh:mm" . Eg: "12:00" returns 0 degree.

2- Let's say you have a file which contains numeric values such (4393, 437834, 555232, 534344, etc....)
You need to write a software that reads the file input, and then takes user input and returns back "sibling values", sibling values are the entries in the file which are consisted from the input numeric. For example, if the file contains (1123,1231,312,...etc), and the user searched for 3211 the results will be 1123 and 1231. Your code Mustn't do full scan or permutation(i.e if the search input is 123,it's not allowed to search for 123,132,213,231,312,321 --all possibilities)

Is This A Good Question/Topic? 0
  • +

Replies To: guys ,, I have an assignment

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: guys ,, I have an assignment

Posted 17 April 2013 - 03:33 AM

If you can produce a decent attempt (or even a really bad attempt) to solve these problems I will gladly assist you. If you aren't willing to put in that effort however, then I (and most others) shall do the same.

So for now, no code but simple advice. For number one the angle of each hand is just a ratio.
minutes/minutes_in_hour = angle/degrees_in_circle
For number two, if two sorted lists are equal, then they contain all the same characters.

That should be enough to get started.
Let's see an attempt,
-Mek
Was This Post Helpful? 0
  • +
  • -

#3 101newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-April 13

Re: guys ,, I have an assignment

Posted 17 April 2013 - 04:15 AM

View PostMekire, on 17 April 2013 - 03:33 AM, said:

If you can produce a decent attempt (or even a really bad attempt) to solve these problems I will gladly assist you. If you aren't willing to put in that effort however, then I (and most others) shall do the same.

So for now, no code but simple advice. For number one the angle of each hand is just a ratio.
minutes/minutes_in_hour = angle/degrees_in_circle
For number two, if two sorted lists are equal, then they contain all the same characters.

That should be enough to get started.
Let's see an attempt,
-Mek



Thanks a lot for your reply
Yes your comment is very true and I really appreciate it :)

I have started with the first one and now I reached an idea about the solution but the thing is I am not sure how to write it in python I can write it in C++ because I am learning it from colleague , maybe you can help me with the syntax of python

1 - I will make two variables for converting into digrees :

degree ( int m , int h ) {

float m_digree , h_digree , answer ;

m_digree = ( m *60 + m / 60 ) * 360 / 12 ;
h_digree = ( h *60 + m ) ;

answer = m_digree - h_digree;

cout >> answer ;
Was This Post Helpful? 0
  • +
  • -

#4 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • Posts: 63
  • Joined: 07-October 12

Re: guys ,, I have an assignment

Posted 17 April 2013 - 04:25 AM

I just add a piece of advice, here is how you create a function in Python. The forum is mainly for debugging process, not giving out the code to others.

def function(parameter1, parameter2):
    # Write your program and what you want to do
    return a_value

Was This Post Helpful? 0
  • +
  • -

#5 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: guys ,, I have an assignment

Posted 17 April 2013 - 04:33 AM

Well C++ or not that code doesn't look like it can be correct.

The degree of your minute hand is:
min_ang  = (minutes/60)*360
where minutes is a float.
The degree of your hour hand depends on whether or not we are taking into consideration fractional hours or just integer hours. The actual degree of the hour hand is:
hour_ang = (hours/12)*360
where hours is a float that equals:
(input_hours+minutes/60)%12

As for rewriting your code to python if you really don't know any python syntax I don't think I can (or will) be able to bring you up to speed. Also you need to keep in mind your function input is a string of form "hh:mm". It is not two integers.

-Mek
Was This Post Helpful? 1
  • +
  • -

#6 101newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-April 13

Re: guys ,, I have an assignment

Posted 17 April 2013 - 05:02 AM

ok and the second one I dont know how to start with ,,,
any way ...these are only answers for two exercises it is not like I am going to win one million from this all I needed is help with the answers if possible and helping out does not cost anyone anything ,,, it only brings relief to some one in need in the other side of the planet ..
Many Thanks for all ur valuable comments ... and for all your positive help
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: guys ,, I have an assignment

Posted 17 April 2013 - 05:07 AM

View Post101newbie, on 17 April 2013 - 11:02 PM, said:

helping out does not cost anyone anything


Well it does cost your classmates the credit they deserve for doing the work if you can get the same mark without putting in the effort.

And in the long run it costs you because you will one day end up needing to be able to live up to what it seems you can do and since you can't then you will lose then.

Have a try at writing the Python code. If you won't even try then we really can't help you.
Was This Post Helpful? 2
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: guys ,, I have an assignment

Posted 17 April 2013 - 05:40 AM

101newbie said:

helping out does not cost anyone anything
I was fairly sure I was helping. All you need to do is show effort and you will get your solution. What you won't get is a hand-out.

print(sorted("string"))
print(sorted("gtrsin"))
print(sorted("string")==sorted("gtrsin"))

-Mek
Was This Post Helpful? 2
  • +
  • -

#9 101newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-April 13

Re: guys ,, I have an assignment

Posted 19 April 2013 - 03:06 AM

View PostMekire, on 17 April 2013 - 05:40 AM, said:

101newbie said:

helping out does not cost anyone anything
I was fairly sure I was helping. All you need to do is show effort and you will get your solution. What you won't get is a hand-out.

print(sorted("string"))
print(sorted("gtrsin"))
print(sorted("string")==sorted("gtrsin"))

-Mek


Thanks Mek ,

so after continuous reading this was the result :

f=open ('int.txt','r')
number=f.read()
numbers=number.split("\n")
print ("Please give me a number:",)
n=input()
n2=sorted(n)
for current in numbers:
sort=sorted(current)
if sort==n2:
print (n2)

Now ,, can I get your feedback please ????
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,904
  • Joined: 19-March 11

Re: guys ,, I have an assignment

Posted 19 April 2013 - 04:18 AM

Since whitespace is significant in python, it's absolutely crucial that you post your code formatted correctly and in [code] tags.
Was This Post Helpful? 0
  • +
  • -

#11 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: guys ,, I have an assignment

Posted 19 April 2013 - 07:48 AM

So yes, as mentioned, code tags. Python is meaningless without indentation.

That said, I'm going to assume this is what you meant:
f=open ('int.txt','r')
number=f.read()
numbers=number.split("\n")
print ("Please give me a number:",)
n=input()
n2=sorted(n)
for current in numbers:
    sort=sorted(current)
    if sort==n2:
        print (n2)

Your code is extremely close to almost working. I do believe you wanted to print current though and not n2. Also as it would appear your data file has a single number per line, I would rather see you iterate through the file than read the whole thing and split on the newline character.

(following code is python 3; use raw_input for python 2.x)
siblings = []
search=input("Please give me a number: ")
with open("int.txt") as myfile:
    for current in myfile:
        current = current.strip()
        if sorted(current)==sorted(search):
            siblings.append(current)

print(siblings)

-Mek
Was This Post Helpful? 2
  • +
  • -

#12 101newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-April 13

Re: guys ,, I have an assignment

Posted 19 April 2013 - 09:51 AM

noted with thanks

Mek , what about exceptions like if no file was found ?
and please to check my code for the clock :
print ("Please give me a number:",)
number = input()
num=number.split(":")
hour=(num[0])
minute=(num[1])
min_ang = ((minute/60)*360)
hour_ang = (hour/12)*360
if hour_ang>=360:
hour_ang=0
angle=abs(hour_ang-min_ang)
print (angle)

This post has been edited by macosxnerd101: 19 April 2013 - 10:00 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Re: guys ,, I have an assignment

Posted 19 April 2013 - 10:00 AM

You can surround your code in code tags, like so: :code:.

Please do not further surround your code in tags like the il tag.
Was This Post Helpful? 1
  • +
  • -

#14 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: guys ,, I have an assignment

Posted 19 April 2013 - 10:06 AM

You missed your end code tag. Use the button provided on the toolbar.

Anyway... the split is correct. After you split though, if you need to do math with those numbers (which you do) you will have to convert them to ints or floats. Currently they stay strings.

hours,minutes = time.split(":")
hours = float(hours)
minutes = float(minutes)

After changing that your code seems to work. Keep in mind what I said previously though. The hour hand doesn't sit on the hour exactly. Your teacher may or may not care about this, but if you want to be realistic it is easy to take into account.

After converting the numbers properly you should convert this into a function.

As for exceptions in your previous. Honestly if the file you load doesn't exist an exception is exactly what I would want. If you were to catch the exception, without a valid file, you couldn't continue your program anyway.

-Mek
Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,904
  • Joined: 19-March 11

Re: guys ,, I have an assignment

Posted 19 April 2013 - 10:11 AM

IT's a good idea to handle exceptions, as you suggest. If the file can't be found, you'd want to report this to the user and ask them to re-enter the file name.
Read this for a good overview of how to do this.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2