No results found for the specified position. Format a string by grouping characters fou... Python Beginner Level Implementation Mock Interview

MockQuestions

Python Beginner Level Implementation Mock Interview

Question 2 of 11 for our Python Beginner Level Implementation Mock Interview

Get More Information About Our Python Beginner Level Implementation Interview Questions

Question 2 of 11

Format a string by grouping characters found in the string into similar lengths.

This question tests that a developer is familiar with Python textual data.

You are given a string named string consisting of alphanumeric characters. It contains some dashes - at random places dividing the string into groups. These groups can be of different or the same length. Your task is to format the string such that the dashes divide the string into groups of equal length K. The first group may have a length of less than or equal to K but the rest of the groups must have length K.

/*Example*/

Given - “abc-def” and K = 2
Expected output - “ab-cd-ef”
All groups have the same length 2.

Given - “abc-def-abdj-k” and K = 3
Expected output - “ab-cde-fab-djk”
All groups have except the first group has the same length 3.

Solution:

Given string - string
Let, the normal length of groups (except for the first group) be K
Let, the length of the given string be L
Let, the count of alphanumeric characters be N
If K divides N, then there are exactly K // N such groups in the formatted string each of length K.
If K does not divide N, then there are K // N groups each of length K and one group with remaining characters. The first group will have fewer than K characters and the rest will have exactly K characters. It is guaranteed that its length will be at least 1 because K does not divide N. We can obtain the remaining characters using modulo operator K % N.
Let’s understand the sample inputs.
Given string- “abc-def”
Given K = 2

N = 6
Since K divides N, there will be 6 / 2 = 3 groups in the formatted string.

Given string- “abc-def-abdjk”
Given K = 3

N = 11
Since K does not divide N, there will be 11 // 3 = 3 groups in the formatted string with length K = 3 viz. cde, fab, djk. The remaining two characters ab will form the first group with length <= K = 3.

def formatLicenseKey(string, k):
	'''
		separate elements from string which can't be 
		part of block of size	k each.
	'''
	# remove all '-' from string and concat back 
	# to get simple string
	hypen_removed_string = ''.join(string.split('-'))

	length = len(hypen_removed_string)

	# get the number of char which can't fit in block 
	# of k elements each
	extra_chars = length % k

	# get extra characters from front of string
	prefix = hypen_removed_string[:extra_chars]
	remaining = hypen_removed_string[extra_chars:]
	# make chunks of size k 
	lst = []
	for i in range(0, len(remaining), k):
		 lst.append( remaining[i : i + k] )
 
	remaining_string = '-'.join(lst)

	if prefix:
		return prefix + '-' + remaining_string
	return remaining_string

Time complexity- O(n)
Space complexity- O(n)

Written by on May 21st, 2021

Next Question

How to Answer: Format a string by grouping characters found in the string into similar lengths.

Advice and answer examples written specifically for a Python Beginner Level Implementation job interview.

  • 2. Format a string by grouping characters found in the string into similar lengths.

      This question tests that a developer is familiar with Python textual data.

      You are given a string named string consisting of alphanumeric characters. It contains some dashes - at random places dividing the string into groups. These groups can be of different or the same length. Your task is to format the string such that the dashes divide the string into groups of equal length K. The first group may have a length of less than or equal to K but the rest of the groups must have length K.

      /*Example*/
      
      Given - “abc-def” and K = 2
      Expected output - “ab-cd-ef”
      All groups have the same length 2.
      
      Given - “abc-def-abdj-k” and K = 3
      Expected output - “ab-cde-fab-djk”
      All groups have except the first group has the same length 3.

      Solution:

      Given string - string
      Let, the normal length of groups (except for the first group) be K
      Let, the length of the given string be L
      Let, the count of alphanumeric characters be N
      If K divides N, then there are exactly K // N such groups in the formatted string each of length K.
      If K does not divide N, then there are K // N groups each of length K and one group with remaining characters. The first group will have fewer than K characters and the rest will have exactly K characters. It is guaranteed that its length will be at least 1 because K does not divide N. We can obtain the remaining characters using modulo operator K % N.
      Let’s understand the sample inputs.
      Given string- “abc-def”
      Given K = 2

      N = 6
      Since K divides N, there will be 6 / 2 = 3 groups in the formatted string.

      Given string- “abc-def-abdjk”
      Given K = 3

      N = 11
      Since K does not divide N, there will be 11 // 3 = 3 groups in the formatted string with length K = 3 viz. cde, fab, djk. The remaining two characters ab will form the first group with length <= K = 3.

      def formatLicenseKey(string, k):
      	'''
      		separate elements from string which can't be 
      		part of block of size	k each.
      	'''
      	# remove all '-' from string and concat back 
      	# to get simple string
      	hypen_removed_string = ''.join(string.split('-'))
      
      	length = len(hypen_removed_string)
      
      	# get the number of char which can't fit in block 
      	# of k elements each
      	extra_chars = length % k
      
      	# get extra characters from front of string
      	prefix = hypen_removed_string[:extra_chars]
      	remaining = hypen_removed_string[extra_chars:]
      	# make chunks of size k 
      	lst = []
      	for i in range(0, len(remaining), k):
      		 lst.append( remaining[i : i + k] )
       
      	remaining_string = '-'.join(lst)
      
      	if prefix:
      		return prefix + '-' + remaining_string
      	return remaining_string

      Time complexity- O(n)
      Space complexity- O(n)

      Written by Jennie Taylor on May 21st, 2021