473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Writting a function

How would i write a function that prints all numbers in the range a to b(inclusive) that have all digits belonging to the set (1,3,4,8,9). The function takes two integer arguments:A and B...


I understood the question and i have seen an example with the (dictionary.txt), but it was to check vowels in words. So for this one i don't know how i would check for those numbers in the range which the user wishes....Lets say from 10000 to 1,000,000
Oct 14 '10 #1
1 1408
bvdet
2,851 Expert Mod 2GB
The first hurdle is to convert the number to a list of the individual digits and create a set object from the list. Then you can do set1.issubset(s0) to determine if all the digits are in the set (1,3,4,8,9). There are a couple of ways to do this off the top of my head. This is probably the easiest:
Expand|Select|Wrap|Line Numbers
  1. >>> n = 1234567890
  2. >>> list(str(n))
  3. ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
  4. >>> map(int, list(str(n)))
  5. [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
  6. >>> [int(i) for i in list(str(n))]
  7. [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
  8. >>> set([int(i) for i in list(str(n))])
The other way is a loop using divmod():
Expand|Select|Wrap|Line Numbers
  1. def numtodigits(n):
  2.     output = []
  3.     while True:
  4.         a,b = divmod(n, 10)
  5.         output.insert(0, b)
  6.         n = a
  7.         if not a: return output
  8. '''
  9. >>> numtodigits(1230)
  10. [1, 2, 3, 0]
  11. >>> 
  12. '''
Oct 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ken | last post by:
I am getting this error from a gcc compile and I was wondering whether this was 100% valid. This seems a little extreme to me the c++ cast operators appear to only work on objects which defeats...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
12
by: Baldy | last post by:
really tricky one here. I want to be able to call a function but the function name is in a variable. The code is a security module that is querying a table that stores names of functions, forms,...
0
by: Hai Nguyen | last post by:
Hi everyone I'm writting a javascript for my validators. Some of them will be validated or server side, some of them will be validated on client side. Since I don't want to mix my javascript...
11
by: Geagleeye | last post by:
Hi, Im a rookie in vba, and therefor got a problem. i do have folwing records F G --------------- 2 3 2 5 6 7
6
by: noridotjabi | last post by:
Is there any way in C to write source which would make an executable that excepted modifications. For instance say we have modable program that excepts plugins. That starts with a menu. 1)Add...
3
by: howachen | last post by:
I have a function foo(), I want all my page can call this method without include / require I know I can set in php.ini by setting the auto_prepend... but this will cause overhead as every file...
6
by: Java1963 | last post by:
Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- ...
1
by: rahulbsbs | last post by:
Sir how can i get the ip address of the remote system by writting a java code,ie i want to get ip address of the remote system that is connected to my server computer ,the ip address must be...
6
by: Alan Coats | last post by:
I have the following function in php which works out a cut of date which is the Tuesday the week prior to the actual date. As I am new to C#, any suggestions on how to do similar in C#? function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.