perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 372 - Task 1: Rearrange Spaces

 1 #!/usr/bin/env perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-372/#TASK1
 3 #
 4 # Task 1: Rearrange Spaces
 5 # ========================
 6 #
 7 # You are given a string text of words that are placed among number of spaces.
 8 #
 9 # Write a script to rearrange the spaces so that there is an equal number of
10 # spaces between every pair of adjacent words and that number is maximised. If
11 # you can’t distribute, place the extra spaces at the end. Finally return the
12 # string.
13 #
14 ## Example 1
15 ##
16 ## Input: $str = "  challenge  "
17 ## Output: "challenge    "
18 ##
19 ## We have 4 spaces and 1 word. So all spaces go to the end.
20 #
21 ## Example 2
22 ##
23 ## Input: $str = "coding  is  fun"
24 ## Output: "coding  is  fun"
25 ##
26 ## We have 4 spaces and 3 words (2 gaps). So 2 spaces per gap.
27 #
28 ## Example 3
29 ##
30 ## Input: $str = "a b c  d"
31 ## Output: "a b c d "
32 ##
33 ## We have 4 spaces and 4 words (3 gaps). So 1 space per gap and 1 remainder.
34 #
35 ## Example 4
36 ##
37 ## Input: $str = "  team      pwc  "
38 ## Output: "team          pwc"
39 ##
40 ## We have 10 spaces and 2 words (1 gap). So 10 spaces per gap.
41 #
42 ## Example 5
43 ##
44 ## Input: $str = "   the  weekly  challenge  "
45 ## Output: "the    weekly    challenge "
46 ##
47 ## We have 9 spaces and 3 words (2 gaps). So 4 spaces per gap and 1 remainder.
48 #
49 ############################################################
50 ##
51 ## discussion
52 ##
53 ############################################################
54 #
55 # We count the number of spaces first using s///g to replace all
56 # whitespace in a copy of the string. Then we remove all whitespace
57 # at the beginning of the string and at the end, so when splitting at
58 # whitespace, we only get back a list of words.
59 # If that list has at most one word, we return it with all whitespace
60 # appended at the end.
61 # Otherwise, we calculate the number of spaces per gap and the remaining
62 # whitespace after substracting all of the whitespaces that go into the
63 # gaps, then we piece together the final output from that.
64 
65 use v5.36;
66 
67 rearrange_spaces("  challenge  ");
68 rearrange_spaces("coding  is  fun");
69 rearrange_spaces("a b c  d");
70 rearrange_spaces("  team      pwc  ");
71 rearrange_spaces("   the  weekly  challenge  ");
72 
73 sub rearrange_spaces($str) {
74     say "Input: \"$str\"";
75     my $tmp = $str;
76     my $spaces = $tmp =~ s/\s//g;
77     $str =~ s/^\s+//;
78     $str =~ s/\s+$//;
79     my @parts = split /\s+/, $str;
80     if(scalar(@parts) <= 1) {
81         return say "Output: \"" . join("", @parts) . ' 'x$spaces . "\"";
82     }
83     my $spaces_per_gap = int($spaces / ( scalar(@parts) - 1 ));
84     $spaces -= $spaces_per_gap * ( scalar(@parts) - 1 );
85     say "Output: \"" . join(' 'x$spaces_per_gap, @parts) . ' 'x$spaces . "\"";
86 
87 }