Skip to content

Commit 579a115

Browse files
authored
Merge pull request #12788 from vinodk89/branch-for-challenge-341
Solutions for Challenge-341
2 parents 5fab196 + 39aa246 commit 579a115

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

challenge-341/vinod-k/perl/ch-1.pl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
# Read sentence from standard input
7+
print("Enter the sentence:\n");
8+
chomp(my $str = <STDIN>);
9+
10+
# Read broken keys as space-separated input from standard input
11+
print("Enter the broken keys as space-separated:\n");
12+
chomp(my $keys_line = <STDIN>);
13+
my @keys = split /\s+/, $keys_line;
14+
15+
my %broken = map { lc($_) => 1 } @keys;
16+
my @words = split /\s+/, $str;
17+
18+
my $count = 0;
19+
foreach my $word (@words) {
20+
my $can_type = 1;
21+
foreach my $char (split //, lc($word)) {
22+
if ($broken{$char}) {
23+
$can_type = 0;
24+
last;
25+
}
26+
}
27+
$count++ if $can_type;
28+
}
29+
30+
print "$count\n";

challenge-341/vinod-k/perl/ch-2.pl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
print "Enter the string: ";
7+
chomp(my $str = <STDIN>);
8+
9+
print "Enter the character to reverse prefix until: ";
10+
chomp(my $char = <STDIN>);
11+
12+
my $index = index($str, $char);
13+
14+
if ($index == -1 || $index == 0) {
15+
print "$str\n";
16+
} else {
17+
my $prefix = substr($str, 0, $index + 1);
18+
my $reversed = reverse $prefix;
19+
print $reversed . substr($str, $index + 1) . "\n";
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sentence = input("Enter the sentence: ")
2+
keys_input = input("Enter the broken keys separated by space: ")
3+
4+
broken_keys = set(keys_input.lower().split())
5+
6+
count = 0
7+
for word in sentence.split():
8+
if all(char not in broken_keys for char in word.lower()):
9+
count += 1
10+
11+
print(f"Number of words that can be typed: {count}")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
input_str = input("Enter the string: ")
2+
char = input("Enter the character to reverse prefix until: ")
3+
4+
index = input_str.find(char)
5+
6+
if index == -1 or index == 0:
7+
# No change if character not found or at start
8+
result = input_str
9+
else:
10+
prefix = input_str[:index+1]
11+
rest = input_str[index+1:]
12+
result = prefix[::-1] + rest
13+
14+
print(f"Resulting string: {result}")

0 commit comments

Comments
 (0)