In [12]:
# test1
def count_word(s: str) -> str:
    """
    Given a string (no space), the output is a countting of the chars. For exampel:

    'assststttb&'  ->  'a1s4t4b1&1'
    ''            ->  ''
    """



# test 2
def count_setp(m: int) -> int:
    """
    The road map for the rabit is a list with length of m (m >= 1), the rabit can either jump 1 step or 2 steps per jump. How many jumping methods are there for the rabit reach the  end (never jump back).
    """
    a, b = 0, 1
    for _ in range(m):
        a, b = b, a+b
    return b


# test1
def count_word(s: str) -> str:
    """
    Given a string (no space), the output is a countting of the chars. For exampel:

    'asssstttb&'  ->  'a1s4t3b1&1'
    ''            ->  ''
    """
    dict = {}
    key =[]
    for i in s:
      if s in dict:
        dict[s] +=1
      else:
        dict[s] = 1
        key.append(s)
  	return ''.join([str(i) +str(dict[i]) for i in key])
  
  	''	-> ''
    'a' -> 'a1'
    'asa' -> 'a2s1'
    '%%Aa' -> '%2A1a1' 
    
# test 2
def count_setp(m: int) -> int:
    """
    The road map for the rabit is a list with length of m (m >= 1), the rabit can either jump 1 step or 2 steps per jump.
    How many jumping methods are there for the rabit reach the  end (never jump back).
    """
    # f(n) = f(n-1) + f(n-2)
    if m == 0:
      return 0
    elif m == 1:
      return 1
    else:
      dp = [0]*(m+1)
      dp[0]=1
      dp[1]= 1
      for i in range(2,m+1):
        dp[i] = dp[i-1]+dp[i-2]
      return dp[m]
    
    
    0 -> 0
    1 -> 1
    2 -> 2
    3 -> 3
    
    # test1
def count_word(s: str) -> str:
    """
    Given a string (no space), the output is a countting of the chars. For exampel:

    'asssstttb&'  ->  'a1s4t3b1&1'
  	'aaabbbaaab'  ->  'a6b4'
    ''            ->  ''
    """
    letters = list(s)
    letter_count_dict={}
    
    for letter in letters:
      if letter in letter_count_dict:
        letter_count_dict[letter]+=1
      else:
        letter_count_dict[letter]=1
        
  
	unique_letters=[]
    
    for letter in letters:
      if letter in unique_letters:
        pass
      else:
        unique_letters.append(letter)

    
    for letter in unique_letters:
      string_to_return=string_to_return+letter+str(letter_count_dict[key])
      
    return string_to_return
        
    
  
count_word("")

count_word("aaaaaaaaaa")

count_word("abc")

count("aaiijeww")

In [17]:
# test
print(count_setp(m=4))

# test 2
b = count_word(s='aaa')
print(b)


5
None

In [3]:
def count_word(s: str) -> str:
    """
    Given a string (no space), the output is a countting of the chars. For exampel:

    'asssstttb&'  ->  'a1s4t3b1&1'
    ''            ->  ''
    'aA'          ->  'a1A1'
    """
    # CODE HERE PLEASE
    l = len(s)
    if l == 0:
        return ''
    if l == 1:
        return s + '1'
    r = ''
    cnt = 1
    i = 1
    while i < l:
        if s[i] == s[i-1]:
            cnt += 1
        else:
            r = r + s[i-1] + str[cnt]
            cnt = 1
        
        i += 1
    r = r + s[i-1] + str[cnt]
    return r

In [4]:
r = count_word('abcd')
print(r)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-d5b24d36d26b> in <module>()
----> 1 r = count_word('abcd')
      2 print(r)

<ipython-input-3-927783cd49d1> in count_word(s)
     20             cnt += 1
     21         else:
---> 22             r = r + s[i-1] + str[cnt]
     23             cnt = 1
     24 

TypeError: 'type' object is not subscriptable

In [9]:
def count_the_string(str):
    res = []
    for c in str:
        if 0 != len(res) and res[-1][0] == c:
            res[-1] = (c, res[-1][1] + 1)
        else:
            res.append((c, 1))
    return ''.join('%s%d' % x for x in res)

print(count_the_string('aaabbba'))


a3b3a1

In [18]:
def count(s: str) -> str:
    """
    'aabba' -> 'a3b2'
    """
    d = {}
    key =[]

    for ss in s:
        if ss in d:
            d[ss] +=1
        else:
            d[ss] = 1
            key.append(ss)

    return ''.join([str(ss) + str(d[ss]) for ss in key])

print(count('aabbaaa'))


a5b2

In [ ]: