Python Forum
container data type formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
container data type formatting
#1
hello,

one interesting task:
>>> gaga(15)
[ 1, 2, 'gaga', 4, 'bieber', 'gaga', 7, 8, 'gaga', 'bieber', 11, 'gaga', 13, 14, 'gagabieber' ]
until now i have managed to:
 
def gaga(n):
    n = list(range(1, n + 1))
    s = ['gaga' if cm % 3 == 0 else cm for cm in n]
    return s
 
def gaga(n) -> gabi.py
it works in the interactive mode of the interpreter:
>>> import gabi
gabi.gaga(15)
[1, 2, 'gaga', 4, 5, 'gaga', 7, 8, 'gaga', 10, 11, 'gaga', 13, 14, 'gaga']
then i try in some unfolded form:
def gaga(n):
    n = list(range(1, n + 1))
    for cm in n:
        
        if cm % 3 == 0:
            cm % ('gaga')

        elif cm % 5 == 0:
            cm % ('gagabieber')

    print(cm) 
but it does not work. some ideas?
thanks in advance!
Reply
#2
if it is divisible by 3 - print gaga
if it is divisible by 5 - print bieber
if it is divisible by both 3 and 5 - print gagabieber
I will leave to you, how to check and in what order
Reply
#3
(Jan-26-2018, 09:18 AM)nzcan Wrote:             cm % ('gaga')
What is the result of 3 % ('gaga') ?
Reply
#4
hi,
i have found one solution of the fizzbuz task that works good.
def fb(n):
	
	n = list(range(1, n +1))
	
	for cm in n:

		if cm % 3 == 0 and cm % 5 == 0:
			print('fizzbuz')
		elif cm % 3 == 0:
			print('fizz')
		elif cm % 5 == 0:
			print('buzz')
		else:
			print(cm)
this solution saved in a fizz_t.py file can be imported in to the interactive mode of the interpreter
and after we call it, it will return a for-loop iteration:
>>> import fizz_t
>>> fizz_t.fb(16)
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuz
16
>>> 

how the fb-function should look like if we want that it returns not a for-loop iteration, but a list like in:
>>> fizz_t.fb(15)
[ 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz' ]

thanks in advance
Reply
#5
i found the solution of this task, but it is pretty unexpected and complicated!
Reply
#6
Here is a version with two functions
def gabize(n, a, b):
    if n % 15 == 0:
        return a + b
    elif n % 3 == 0:
        return a
    elif n % 5 == 0:
        return b
    else:
        return n
    
def func(n, a='gaga', b='bieber'):
    return [gabize(i, a, b) for i in range(1, n + 1)]

if __name__ == '__main__':
    print(func(15))
    print(func(15, 'fizz', 'buzz'))
Output:
[1, 2, 'gaga', 4, 'bieber', 'gaga', 7, 8, 'gaga', 'bieber', 11, 'gaga', 13, 14, 'gagabieber'] [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with data type timthebouncer 7 3,186 Apr-15-2020, 05:43 PM
Last Post: timthebouncer
  Formatting data based on DataFrames values 577e94982d620b84f7c536d5e76f1e 0 2,039 Dec-03-2018, 02:23 AM
Last Post: 577e94982d620b84f7c536d5e76f1e

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020