Skip to content

Instantly share code, notes, and snippets.

@maelfosso
Last active April 7, 2018 10:48
Show Gist options
  • Save maelfosso/139844d7b28e2ee07b4afbf03efa4598 to your computer and use it in GitHub Desktop.
Save maelfosso/139844d7b28e2ee07b4afbf03efa4598 to your computer and use it in GitHub Desktop.
Chef decided to find the connections with all of his friends in an unnamed social network. He calls a user of the social network his friend if there is a common substring of the string "chef" and the nickname of that user with length ≥ 2. Given a list of users of the social network, compute the number of Chef's friends.

Introduction

The problem comes from codechef

It is an example of how we will work in GDG Yaounde

My Solution

  1. I create a vector containing substrings of length upper than 2 from the word chef.
  2. After reading all the username, I loop through them and and if an username contains one of the substring inside the vector create above then I will increment the result

Testing

  • Install Python 3
  • Run python3 frk.py
import sys
chef_sub_str = [
'ch', 'che', 'chef',
'he', 'hef',
'ef'
]
# Read the number of potential friends
N = int(input())
if not(1 <= N and N <= 5000):
print("Error - ", N)
# Read each friend and put it into the array
users = []
for i in range(N):
u = str(input())
if not(3 <= len(u) and len(u) <= 20) :
sys.exit("Erreur ", u)
users.append(u)
# Find connections
result = 0
for u in users:
if any(s in u for s in chef_sub_str):
result = result + 1
# Print results
print(result)
@maelfosso
Copy link
Author

Success !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment