1

I need help to understand this question. Please don't post the answer, only the ways to solve it.

Assign 10 to the variable base. Assign the set {0,1,2,3,4,5,6,7,8,9} to the variable digits. Now write an expression using a comprehension and base and digits whose value is the set of all at-most- three-digit numbers. Your expression should work for any base. For example, if you instead assign 2 to base and assign {0,1} to digits, the value of your expression should be {0,1,2,3,4,5,6,7} because this is the set of numbers that, base two, have at most three digits.

I try this expression but I could not solve the base 2 question.

base = 10
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{(x*(base**2))+(y*(base**1))+(z*(base**0)) for x in digits for y in digits for z in digits if (x*(base**2))+(y*(base**1))+(z*(base**0))>((y*(base**1))+(z*(base**0)))}
5
  • 2
    I would recomend finding the largest value in the set of digits. I would then just do range(int('%s%s%s'%(max_digit,max_digit_max_digit),base)+1) but that probably isnt what your teacher wants Commented Jul 8, 2013 at 20:13
  • I need to come up with a comprehension that return a {100-999} set for decimals and the same expression should work with any base ex: 2,8 16... Commented Jul 8, 2013 at 20:20
  • 2
    I recognise this question as being from the Coursera matrix/linear algebra course. You should really look on the course discussion board there, rather than asking for help here. Commented Jul 8, 2013 at 20:27
  • your code seems to work fine ... "At most 3 characters" not "exactly 3 characters" ... just get rid of the if in the comprehension Commented Jul 8, 2013 at 20:27
  • Actually you need to come up with a generator that will give you all the three digit numbers in the range 001 to MMM where M is max digit this will automatically include the one and two digit numbers by allowing leading 0s. The other hints look at the output of python -c "print help(int)"... Commented Jul 8, 2013 at 20:29

3 Answers 3

2

...whose value is the set of all at-most- three-digit numbers.

Emphasis added. You need to include the values 000 - 099 (for base = 10).

It looks like you've almost got it. You don't need a filter in your comprehension. Think about how many results you'll get from for x in digits for y in digits for z in digits for different values in digits. It should be exactly the right number of values.

Sign up to request clarification or add additional context in comments.

Comments

1

You already had the answer, simply remove the if statement. Try this:

{x*base**2+y*base**1+z*base**0 for x in digits for y in digits for z in digits}

If you use base 10 and digits (0,1,2,3,4,5,6,7,8,9) you should get a list from from 0 to 999. And if you use base 2 and digits (0,1) you should get a list from 0 to 7.

Comments

1

As I guess your homework is done, almost two years later:

The number of 3 digits number within your base is a sequence of the form

base^digits

For example, to have the number of 3-digits numbers in base ten, one would type in python:

10**3

You then can use the range function to have them all:

{x for x in range(10**3)}

This works with any base.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.