Is the max() function only available when you have the math library imported already and can you use it with letters or numbers (like I've done in javascript)


#1

The max function imports the highest in a set of numbers:

example:

maximum = max(1,2,3)

print maximum

The result will be 3.

My question is asking if you need to import the math library first before using max. I assume no, but I want to make sure with someone that has more experience.


#2

max is one of Python’s “built in” functions, and so is always available without you having to import a module.

The full list is here:

https://docs.python.org/2/library/functions.html

:slight_smile:


#3

can you use the min function like you use the following in javascript:

FIRST FROM PAARL
Write a function called firstPaarl that takes a comma separated string parameter with registration numbers and returns the first registration number in the string for Paarl. Remember Paarl registration numbers starts with CJ.

Write your code here
function firstPaarl(firstReg){
 console.log(firstReg);
  var cjStorer = '';
  var stringOrig = firstReg.split(', ');
 
  for (var i = 0; i < stringOrig.length;i++) {
    if (stringOrig[i].startsWith('CJ')) 
        { cjStorer = stringOrig[i]; break; }
  
    // console.log(emptyList);
                                          
  }
    

return cjStorer
};

Note: the above is javascript and paarl (in RSA) start with CJ and then numbers

Feel free to ask me to clarify anything.

Here is the task I am on:
find s in yes exercise


#4

JavaScript isn’t my forté, but I think I can follow that.

Looks like you’re just splitting a comma-delimited string, then iterating each item from the string to find the first item which starts with “CJ”.

What I can’t figure out, unless I’m missing something obvious, is why you need the min function in Python to make an equivalent of that JavaScript?

I noticed you’re edited the topic of the thread as well; to answer your question, yes the min and max functions will work on strings as well as numbers.


#5

I was thinking if you had the same data that you could use min and use a for loop to get the same result?

I know I’m missing something…but I think min will be splitting the string?

Maybe I used an incorrect example…

How about this onme:

Intrucitons:
WEEKEND OR NOT
Write a function called weekOrWeekend that takes in a day parameter. If the day passed in is a weekDay it should return ‘week’ otherwise is should return ‘weekend’;

Start using the assert statement to test your functions.

Assert statements are used to state facts about your code. Scenarios and statements that you expect to be either true or false can be created. When an assert pass nothing happens, only when an assert fails it will report an error.

Add tests for your script like this.

// after you declared the function
assert.equal(weekOrWeekend(‘Wednesday’), ‘week’);
assert.equal(weekOrWeekend(‘Sunday’), ‘weekend’);
The assert function will fail and print an error to the screen if the expected value is not returned from the function.

function weekOrWeekend (day){

  var week = !day.startsWith('S');

  var weekend = day.startsWith('S');

  if (week === true){return "week";}

  else {return "weekend";}

};

In this example you can see that saturday and sunday both start with S. So if you turn all the day of the week into an array and use min() in the array for the first position — in order to find s, would that be a correct usage of min?

Obviously other built in functions that I do not know would be helpful in this situation. for example, how to turn a string into an array.

I did not mention, all the item would be stored in a string like this:

days='monday,tuesday,wedneday,thursday,friday,saturday,sunday

at the beginning, prior to you seperating, etc. in a function.


#6

Ahhh, sorry - I see what you mean now. :slight_smile:

No, that wouldn’t work how you wanted. If you called min("Saturday"), the output would be “S”. If you called min("saturday"), you’d get “a”. I presume that this is due to Python evaluating the ASCII code for the characters, and as capitals have a lower ASCII value than lowercase letters the capital would be favoured… (That’s a guess, though. I’d need to go digging in the technical docs.)

In Python, a string is already an array; you can iterate through a string or retrieve one or more characters by index…

my_word = "Saturday"

# iterate over the string and print each letter
for letter in my_word:
  print letter

# grab the 3rd letter
print my_word[2]

# grab the 4th, 5th and 6th letters
print my_word[3:6]

Does that help? :slight_smile:


#7

thats a great start. I see what you’re saying but I think I will need to talk it over with one of my mentors haha. But Im glad I’m on the right track.


#8

As you can tell, I’m learning still. I’ve already done javascript so by doing python I’m hoping to review those core programming concepts with python and cement them haha. I spent 4 months learning javascript! It took forever!


#9

No worries.

Keep in mind, though, that Python already includes a startswith() method as part of the string type.

https://docs.python.org/2/library/stdtypes.html#str.startswith

The resulting code in Python wouldn’t be too different to in JavaScript, then. :slight_smile:

I don’t think anyone ever really stops learning when it comes to programming. There’s always room to learn a new way of doing something, or picking up little tips and tricks from others and so on. :slight_smile:


#10

Yes! I’ve already seen that! I was o proud of myself for completing javascript. Now I am learning there are even shorter way to accomplish the same tasks that used to take me hours! For some of the exercies, I would spend at least 6 hours. now my time is down to half an hour! (obviously based on those tips from my mentors and you guys!)


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.