Author Topic: Reaallllly simple question  (Read 3138 times)

0 Members and 1 Guest are viewing this topic.

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Reaallllly simple question
« on: June 30, 2010, 11:54:58 am »
In python, why does:
Code: [Select]
>>> print('hello')
hello
and:
Code: [Select]
>>> print(variable1)
23
but:
Code: [Select]
>>> print('hello', variable1)
('hello', '23')
And perhaps more importantly, what do I do to get: hello 23

ROFL
Nevermind, I did it. Just remove the brackets.
LOL, what is it with me and working something out the second I ask about it!!
« Last Edit: June 30, 2010, 11:57:36 am by Sophia »

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1435
  • Karma: 10023
  • Gender: Male
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #1 on: June 30, 2010, 12:33:27 pm »
"()" is a 'tuple' constructor.

i.e. it effectively constructs a read-only list from the parameters provided. If you "print" a tuple (or list), it will show you it's contents essentially in the form used to create it in the first place. Try;

Code: [Select]
a=(1,2,3,4,5)
print a
print a[1]
a[1]=9

The last line will error as being a tuple, it's read-only. If you try;

Code: [Select]
a=[1,2,3,4,5]
print a
print a[1]
a[1]=9

The results will be similar except the last line won't error, "[]" is a list constructor. (lists are read/write)

To really bend your mind a little "(1)" will not generate a tuple as it thinks it's an arithmetic expression, i.e. a "1" in brackets .. "(1,2)" will generate a tuple because it's obviously not an arithmetic expression, so if you want a tuple containing a single numeric entry, you need to write "(1,)". An example of when this is useful;

Code: [Select]
#Add an additional digit to a tuple;
a=(1,2,3)
a+4 # will not work
a+(4) # will not work
a+(4,) , will work and yields (1,2,3,4)

Exercise, work out why / how this works;
Code: [Select]
a=('a','b','c')
"-".join(a)

MP Loves Python ..  :D

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #2 on: June 30, 2010, 01:19:54 pm »
I'm probably way off here, but is it because the ('a','b','c') is the list but the contents are the strings a b and c and the joining is happening outside of the strings, like is the read only part the strings themselves rather than the whole construct?
/making a fool of herself

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1435
  • Karma: 10023
  • Gender: Male
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #3 on: June 30, 2010, 01:25:02 pm »
:)

What I was getting at, is that the "-" is a string. Strings are objects, which have a number of methods attached to them, one of which is "join". The join method takes one parameter, a list in this instance, and uses the base object (i.e. "-") to join all the elements of what it was supplied with (i.e. the tuple)

It's worth noting the difference, tuple / list .. subtle but the cause of many bugs!  You can add items to a tuple, but you can't change them (!) Lists, you can add and change ... tuple is "()" , list is "[]" ..  ;)

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #4 on: June 30, 2010, 07:43:48 pm »
That makes sense. And I wasn't so far off really :p

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1435
  • Karma: 10023
  • Gender: Male
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #5 on: July 01, 2010, 10:19:42 pm »
Ok, if you're feeling adventurous ;

Code: [Select]
def factorial(n): return 1 if n==1 else n*factorial(n-1)

For 15% of the marks - what does it do and how would you use it ?
For another 35% - in context, what sort of function is this and what sort of if-test is this ?
For the remaining 50% - what's the methodology's major shortcoming and why would you generally never use it in real programming ?

 ;)

(if you try this and it errors, you need a newer version if Python, get v2.6+)

For a bonus 25%, rewrite the function in one line of "C" ...  :o

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #6 on: July 01, 2010, 10:56:46 pm »
Is this an open book test?
(uh... kind of too late if it isn't...)

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #7 on: July 01, 2010, 11:30:14 pm »
1. uh it finds the factorial which is what you get if you multiply every number up to and including the number in question.

2. Wasn't really sure what you were aiming for. Recursive function? Integer function?

3. fact.factorial(-3) OR fact.factorial(9.3)
   blah blah blah blah
   ...
   RuntimeError: maximum recursion depth exceeded.

4. (AHH one thing at a time! :p) *googles* *looks extremely confused*

UHHH, given I know nothing about this topic and when I tried google I found impenetrable things I hereby declare that if you void all rights to laugh at me if this is completely wrong.

int factorial(int n)
{
   if (n == 1)
      return 1
   else
      return 1 * factorial(n-1)
}
   

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1435
  • Karma: 10023
  • Gender: Male
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #8 on: July 02, 2010, 01:26:46 am »
1. 100% - 15 marks
2. 50% - 17.5 marks
3. 50% - 25 marks
4. 0%

Giving you a total of %57.50!   ;)

2. The if-test is an "inline if"

3. You were sort of there with the stack overflow, but I'm thinking maybe for the wrong reason (?) Recursive functions rely on the stack for their variable storage, different languages and indeed different settings within each language vary, so you can never "know" how deeply you can recurse before you experience a stack overflow. If you "know" for example that all Python configurations will recurse to at least a depth of 40 frames, and your maximum requirement is !10, the function is a very elegant solution. However, for general problem solving this sort of specification is not generally something you can rely on and iterative processes that do not have such a limitation, although less 'elegant',  should always be preferred.

4. I'm pretty sure that's not one line (!) although you could write it on one line, the point was to replicate the in-line "if" in "C";

Code: [Select]
int factorial(int n) { return n==1 ? n : n*factorial(n-1); }

Ultimate point being; all languages started out as discrete, as time progresses they're all morphing into "C" one ..
(inline-if is new in Python, appeared in 2.5 as an extension and is available in 2.6 by default)

And you're missing a ";" after both returns, and you have "return 1 * factorial(n-1)" rather than "return n*factorial(n-1)" ... with the added bonus of 1* anything being surplus to requirements .. ;)

For the pink bunny and free tickets for the raffle, can you show the assembler version of the C function ... ;)

Offline shtromm

  • Jr. Member
  • **
  • Posts: 55
  • Karma: 2
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #9 on: July 08, 2010, 06:41:28 am »
1. uh it finds the factorial which is what you get if you multiply every number up to and including the number in question.

2. Wasn't really sure what you were aiming for. Recursive function? Integer function?

3. fact.factorial(-3) OR fact.factorial(9.3)
   blah blah blah blah
   ...
   RuntimeError: maximum recursion depth exceeded.

4. (AHH one thing at a time! :p) *googles* *looks extremely confused*

UHHH, given I know nothing about this topic and when I tried google I found impenetrable things I hereby declare that if you void all rights to laugh at me if this is completely wrong.

int factorial(int n)
{
   if (n == 1)
      return 1
   else
      return 1 * factorial(n-1)
}


The Fraction is the typical example to explain a recursion, but in practice it is never used for this purpose, because the range of int or long-int is reached with a few steps, in practice you are much better off with something like:

Code: [Select]
int FAC (int    iIn)
{
        if ((iIn == 0) || (iIn == 1))
                return 1;
        else if (iIn == 2)
                return 2;
        else if (iIn == 3)
                return 6;
        else if (iIn == 4)
                return 24;
        else if (iIn == 5)
                return 120;

/* etc. */

BTW - if want a faction over R or C, you may have a look into the Gamma-Function http://en.wikipedia.org/wiki/Gamma_function

Offline Sophia

  • Jr. Member
  • **
  • Posts: 72
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #10 on: July 09, 2010, 02:00:36 pm »
I am beginning to regret dropping out of A level maths...

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1435
  • Karma: 10023
  • Gender: Male
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #11 on: July 09, 2010, 04:40:13 pm »
They made us sit through a whole year of two different Hons maths courses, in addition to a numerical analysis course. For what it's worth, 25 years on, the mathematics have proven to be 100% useless to me .. I strongly suspect they taught us the maths because their computing was so crap ... I'm pretty sure that Eigenvalues and Eigenvectors have their commercial applications, but trying to teach it at first year computer geeks does strike me as a monumental waste of time.

I learnt two things at University.

a. The concept of a recursive function.
b. How to drink lots of beer.
c. How poor all some computing academics are at computing

For anyone planning on doing a Uni degree these days and getting themselves into lots of debt, consider that you can teach (a) to a trained monkey in a day and to your average computer student in 5 minutes. So I guess whether it's worthwhile depends to a greater extent how much you like beer ... ;)

Offline shtromm

  • Jr. Member
  • **
  • Posts: 55
  • Karma: 2
  • I've just joined!
    • View Profile
    • Awards
Re: Reaallllly simple question
« Reply #12 on: July 14, 2010, 05:07:32 pm »
Recursions were the great fashion of programmers in 1970s and early 1980s - especially they can look really cool  8) and it was near-by standard with some Pascal programmers to use the recursion anywhere if even remotely possible.

In the 1980s there was a swift, because a recursion is handled via the stack, and all local variables, including function/procedure parameters are stored in the stack and the stack has to be re-winded by the end of the recursion, causing performance problems or stack overflows. Since the dogma shifted and recursions are considered as the absolute No-No.

I personally prefer the middle way and use recursions in certain situations as the most elegant way of doing thinks, but limited.

 

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal