Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52967 > unrolled thread
| Started by | sahil301290@gmail.com |
|---|---|
| First post | 2013-08-24 22:50 -0700 |
| Last post | 2013-08-27 12:03 +0000 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
Checking homogeneity of Array using List in Python sahil301290@gmail.com - 2013-08-24 22:50 -0700
Re: Checking homogeneity of Array using List in Python Chris Angelico <rosuav@gmail.com> - 2013-08-25 16:03 +1000
Re: Checking homogeneity of Array using List in Python Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-25 11:54 -0400
Re: Checking homogeneity of Array using List in Python Dave Angel <davea@davea.name> - 2013-08-25 20:49 +0000
Re: Checking homogeneity of Array using List in Python Neil Cerutti <neilc@norwich.edu> - 2013-08-26 13:49 +0000
Re: Checking homogeneity of Array using List in Python Joshua Landau <joshua@landau.ws> - 2013-08-26 23:39 +0100
Re: Checking homogeneity of Array using List in Python Neil Cerutti <neilc@norwich.edu> - 2013-08-27 12:03 +0000
| From | sahil301290@gmail.com |
|---|---|
| Date | 2013-08-24 22:50 -0700 |
| Subject | Checking homogeneity of Array using List in Python |
| Message-ID | <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> |
I am unable to check homogeneity of Array. I have take Array type Int to be default for my code. Instead of getting Error on NON-INT Values. I want to take input as string. Then check if all input is in (0-9) form, I typecast it into int and Accept. Else, I would like to skip that input. eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] I want it to be interpreted as: [1, [None], [None], [None], 43242] NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-08-25 16:03 +1000 |
| Message-ID | <mailman.210.1377410609.19984.python-list@python.org> |
| In reply to | #52967 |
On Sun, Aug 25, 2013 at 3:50 PM, <sahil301290@gmail.com> wrote: > NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. You'll have to do your own homework, then. Python strongly favours the use of inbuilt functions. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-08-25 11:54 -0400 |
| Message-ID | <mailman.220.1377446073.19984.python-list@python.org> |
| In reply to | #52967 |
On Sun, Aug 25, 2013 at 2:03 AM, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Aug 25, 2013 at 3:50 PM, <sahil301290@gmail.com> wrote: >> NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. You don't make it easy to help you with your homework by leaving out information about what you have learned so far, and perhaps what you have tried that doesn't work Here are some hints: Do you know what the any and all functions do in python? Do you know about list comprehensions or how to write a for loop that iterates over all of the values in a list? If you know about that stuff, think how those skills could be used to solve your problem. This problem is so completely contrived that without knowing the course of your study (what you have learned about python programming so far) it is really useless to try to guess an acceptable answer. Let me suggest you discuss this with your classmates if possible. -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-08-25 20:49 +0000 |
| Message-ID | <mailman.226.1377463819.19984.python-list@python.org> |
| In reply to | #52967 |
sahil301290@gmail.com wrote: > I am unable to check homogeneity of Array. > I have take Array type Int to be default for my code. > > Instead of getting Error on NON-INT Values. But none of them below are int values. > I want to take input as string. > Then check if all input is in (0-9) form, I typecast it into int and Accept. > Else, I would like to skip that input. > > eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] > I want it to be interpreted as: > [1, [None], [None], [None], 43242] > > NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. I don't see any Arrays. You show a list, however. Lists don't have any type for the whole list, only for each element. Seems to me like the minimum functions you'll need are int(), type(), isinstance(). They're built-in, not inbuilt, so perhaps that's okay. Or perhaps you're going to write your own equivalents. But your example contradicts your description enough that I could be wrong about any of the three. > > I want to take input as string. So why do you show it as a list? Do you know how to write a function? Try writing one that takes a string and returns True if all the characters are digits, and False if either: 1) there are no characters 2) there are some non-digit characters Try writing another one that takes a string made up entirely of digit characters, and produces an int from them. Note you'll be doing some multiplies by 10, since presumably you're working in decimal. You could call this function my_int() Write some code that actually illustrates what you're trying to do, show some data being fed into it, and indicate where you ran out of ideas. Then somebody could perhaps help. As it stands, you're referring to types that don't fit, and making assumptions that make no sense. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-08-26 13:49 +0000 |
| Message-ID | <b814mdF7v9aU1@mid.individual.net> |
| In reply to | #52967 |
On 2013-08-25, sahil301290@gmail.com <sahil301290@gmail.com> wrote: > I am unable to check homogeneity of Array. > I have take Array type Int to be default for my code. > > Instead of getting Error on NON-INT Values. > I want to take input as string. > Then check if all input is in (0-9) form, I typecast it into int and Accept. > Else, I would like to skip that input. > > eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] > I want it to be interpreted as: > [1, [None], [None], [None], 43242] > > NOTE: NO INBUILT FUNCTION BE USED. Impossible. I think. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2013-08-26 23:39 +0100 |
| Message-ID | <mailman.248.1377556789.19984.python-list@python.org> |
| In reply to | #53001 |
On 26 August 2013 14:49, Neil Cerutti <neilc@norwich.edu> wrote:
> On 2013-08-25, sahil301290@gmail.com <sahil301290@gmail.com> wrote:
>>
>> eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242']
>> I want it to be interpreted as:
>> [1, [None], [None], [None], 43242]
>>
>> NOTE: NO INBUILT FUNCTION BE USED.
>
> Impossible. I think.
class BoilerplateToStopCheating:
def __init__(self):
"""Factor things out to prevent cheating."""
self.digit_to_number = {"0":0, "1":1, "2":2, "3":3, "4":4,
"5":5, "6":6, "7":7, "8":8, "9":9}
def __call__(self, items):
def fudging():
"""More cheat-fudging."""
for item in items:
try:
as_number = 0
for char in item:
as_number *= 10
as_number += self.digit_to_number[char]
yield as_number
except KeyError:
yield [None]
[*z] = fudging()
return z
converter = BoilerplateToStopCheating()
# Can't use "print"...
# Erm...
converter(['1', ' ', 'asdasd231231', '1213asasd', '43242'])
# Output: [1, [None], [None], [None], 43242]
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-08-27 12:03 +0000 |
| Message-ID | <b83isaFod5aU1@mid.individual.net> |
| In reply to | #53019 |
On 2013-08-26, Joshua Landau <joshua@landau.ws> wrote:
> On 26 August 2013 14:49, Neil Cerutti <neilc@norwich.edu> wrote:
>> On 2013-08-25, sahil301290@gmail.com <sahil301290@gmail.com> wrote:
>>>
>>> eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242']
>>> I want it to be interpreted as:
>>> [1, [None], [None], [None], 43242]
>>>
>>> NOTE: NO INBUILT FUNCTION BE USED.
>>
>> Impossible. I think.
>
> class BoilerplateToStopCheating:
> def __init__(self):
> """Factor things out to prevent cheating."""
> self.digit_to_number = {"0":0, "1":1, "2":2, "3":3, "4":4,
> "5":5, "6":6, "7":7, "8":8, "9":9}
>
> def __call__(self, items):
> def fudging():
> """More cheat-fudging."""
> for item in items:
> try:
> as_number = 0
> for char in item:
> as_number *= 10
> as_number += self.digit_to_number[char]
> yield as_number
>
> except KeyError:
> yield [None]
>
> [*z] = fudging()
> return z
>
> converter = BoilerplateToStopCheating()
>
> # Can't use "print"...
> # Erm...
> converter(['1', ' ', 'asdasd231231', '1213asasd', '43242'])
> # Output: [1, [None], [None], [None], 43242]
Very nice! It seems like unlucky students sometimes get C
programming courses ham-fisted into Python without much thought.
--
Neil Cerutti
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web