惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
J
Java Code Geeks
A
About on SuperTechFans
F
Fortinet All Blogs
B
Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园_首页
博客园 - 叶小钗
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
云风的 BLOG
云风的 BLOG

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Weekly Challenge: Existence
Simon Green · 2026-06-14 · via DEV Community

Simon Green

Weekly Challenge 377

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Reverse Existence

Task

You are given a string.

Write a script to find whether any substring of length 2 is also present in the reverse of the given string.

My solution

This is relatively straight forward. I create a variable reversed_string which is the string reversed. I then have a loop called start_pos that goes from 0 to 2 less than the length of the string. At each position, I check if the two letters at that position are in the original string.

def reverse_existence(input_string: str) -> bool:
    reversed_string = input_string[::-1]
    for start_pos in range(len(input_string)-1):
        if reversed_string[start_pos:start_pos+2] in input_string:
            return True

    return False

The Perl solution follows the same logic.

sub main ($input_string) {
    my $reversed_string = reverse($input_string);
    foreach my $start_pos ( 0 .. length($input_string) - 2 ) {
        if (
            index( $input_string, substr( $reversed_string, $start_pos, 2 ) )
            != -1 )
        {
            say "true";
            return;
        }
    }

    say "false";
}

Examples

$ ./ch-1.py abcba
true

$ ./ch-1.py racecar
true

$ ./ch-1.py abcd
false

$ ./ch-1.py banana
true

$ ./ch-1.py hello
true

Task 2: Prefix Suffix

Task

You are given an array of strings.

Write a script to find if the two strings (str1, str2) in the given array such that str1 is prefix and suffix of str2. Return the total count of such pairs.

My solution

In Python, this is a one line solution. I use the combinations function from the itertools module to produce all combinations of pairs of str1 and str2. For each pair, I use the startwith and endswith function on the strings to see if the criteria is met.

def prefix_suffix(array: list[str]) -> int:
    return sum(
        1
        for str1, str2 in combinations(array, 2)
        if (str1.startswith(str2) and str1.endswith(str2)) or
           (str2.startswith(str1) and str2.endswith(str1))
    )

The Perl solution is a little more complex. I have a function called is_prefix_suffix that checks that s1 starts with an ends with s2.

sub is_prefix_suffix ( $s1, $s2 ) {
    my $l = length($s2);

    return ( substr( $s1, 0, $l ) eq $s2 and substr( $s1, 0 - $l ) eq $s2 )
      ? 1
      : 0;

}

The main function uses the combinations function from the Algorithm::Combinatorics module to generate all possible pairs. It then calls the above function on each order of the pair to see if it is true.

use Algorithm::Combinatorics 'combinations';

sub main (@array) {
    my $count = 0;

    my $iter = combinations( \@array, 2 );
    while ( my $c = $iter->next ) {
        my ( $str1, $str2 ) = @$c;
        if (   is_prefix_suffix( $str1, $str2 )
            or is_prefix_suffix( $str2, $str1 ) )
        {
            ++$count;
        }
    }

    say $count;
}

Examples

$ ./ch-2.py a aba ababa aa
4

$ ./ch-2.py pa papa ma mama
2

$ ./ch-2.py abao ab
0

$ ./ch-2.py abab abab
1

$ ./ch-2.py ab abab ababab
3