<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Seif El-Din Sweilam on Medium]]></title>
        <description><![CDATA[Stories by Seif El-Din Sweilam on Medium]]></description>
        <link>https://medium.com/@saifsweelam?source=rss-2a6dae8a118------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JAEBmpJssAJOsvJKH6WoUQ.jpeg</url>
            <title>Stories by Seif El-Din Sweilam on Medium</title>
            <link>https://medium.com/@saifsweelam?source=rss-2a6dae8a118------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:49:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saifsweelam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing Notifications in a Node.js application using Socket.io]]></title>
            <link>https://medium.com/@saifsweelam/implementing-notifications-in-a-node-js-application-using-socket-io-36931094f19?source=rss-2a6dae8a118------2</link>
            <guid isPermaLink="false">https://medium.com/p/36931094f19</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[real-time-communication]]></category>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Seif El-Din Sweilam]]></dc:creator>
            <pubDate>Mon, 04 Sep 2023 02:35:33 GMT</pubDate>
            <atom:updated>2023-09-04T02:35:33.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e-1VGttXmG6vFShqiartDw.jpeg" /></figure><h3>Introduction</h3><p>Socket.io is an open-source technology for implementing real-time connections through the internet. It’s well-known for its simplicity and straightforwardness which makes it easy to implement functions which require real-time bidirectional connections.</p><p>Socket.io treats the connection between the server and the client as a set of events, where each of them is capable of emitting and listening to events.</p><p>The applications of socket.io are variant as it’s an open-source technology which can be customized to achieve a lot of purposes. It’s commonly used in chat applications and applications that involve self-updated content.</p><p>Today, we are discussing how we can implement notifications functionality through socket.io. To begin, we have a website which contains requests made by users. Any user seeing the request can make an offer to fulfill the request. We need to get the owner of the request notified when an offer is made to his request.</p><h3>Prerequisites</h3><p>To begin, we need to install socket.io to our server and socket.io client to the client applications (in this case we’re using React and Flutter)</p><pre># Server<br>npm install --save socket.io<br># React App<br>npm install --save socket.io-client</pre><pre>dependencies:<br>  socket_io_client: ^1.0.2</pre><h3>Initializing a connection</h3><p>Then we need to make each client join a room labeled by the user ID, so that the server is able to distinguish users and send specific notifications. For this, we will make the client send a request to join a room as soon as they connect.</p><pre>// Server Side<br>io.on(&#39;connection&#39;, (socket) =&gt; {<br>    socket.on(&quot;joinRoom&quot;, (token) =&gt; {<br>        let userData = usersModel.verifyToken(token);<br>        if (!userData) return;<br>        socket.join(userData.id);<br>    })<br>})<br><br>// Client Side<br>const socket = io();<br>socket.on(&#39;connect&#39;, () =&gt; {<br>    socket.emit(&quot;joinRoom&quot;, ACCESS_TOKEN);<br>});</pre><p>In the previous code, the client sends the server the access token so that the server is able to verify the token and get the ID of the user.</p><h3>System Design</h3><p>Now we have got to plan the flow of our notifications system, for this we have made a sequence diagram emphasizing the behavior of the system:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*SOg-yf8vVoSUjX-wGBmfHg.png" /><figcaption>Sequence Diagram for offers and notifications using socket.io</figcaption></figure><p>So now we know the events that we need to implement in order to run our functionality:</p><p><strong>Events emitted by client:</strong></p><ul><li><em>joinRoom</em></li><li><em>createOffer</em></li><li><em>acceptOffer</em></li><li><em>negotiateOffer</em></li><li><em>rejectOffer</em></li></ul><p><strong>Events emitted by server:</strong></p><ul><li><em>newOffer</em></li><li><em>acceptedOffer</em></li><li><em>negotiatedOffer</em></li><li><em>rejectedOffer</em></li></ul><p>Notice that the server events are always a result of the client events, because the users are getting notified about the actions done by other users.</p><p>Whenever a client receives an event, it should render the notification and display it to the user in an appropriate way. This way, the user is getting real-time updates about the request/offer without having to reload or perform any action.</p><h3>Cautions</h3><ul><li>These events involve database operations, so you must be careful to validate all data coming from the client before committing to the database.</li><li>Make sure that the authorization token is valid each time, because you may receive an event from a hacker who is pretending to be some user.</li><li>Make sure to close the socket connection after the user logs off to avoid crashes.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36931094f19" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started with Codeforces as a Python Developer]]></title>
            <link>https://medium.com/@saifsweelam/getting-started-with-codeforces-as-a-python-developer-4c5ea6ad9f04?source=rss-2a6dae8a118------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c5ea6ad9f04</guid>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[competitive-programming]]></category>
            <dc:creator><![CDATA[Seif El-Din Sweilam]]></dc:creator>
            <pubDate>Sat, 08 Jul 2023 16:31:20 GMT</pubDate>
            <atom:updated>2023-07-08T17:15:10.208Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iPZ00kImJY8oVioV5Dy75A.jpeg" /><figcaption>Codeforces logo</figcaption></figure><p><a href="https://codeforces.com/">Codeforces</a> is one of the most recognized websites for practicing competitive programming and participating in Programming Contests. The website has been supported by a great community of developers from all over the world. Being new to the website, everything looks unfamiliar, and you find it difficult to know where to start. You may also encounter many questions about what programming languages to use and other stuff. I’m writing this as I have been through this journey for a while, and I thought I can help you break the ice and say your first word to the weird-looking website.</p><p>I will take you through the website by trying to answer questions that you may think of. This article is more specific for Python Developers.</p><h3>What makes it engaging for developers?</h3><p>Well, you may have been through different websites that are more engaging in terms of the user interface and details that look expensive. That shall make you feel annoyed with the plain interface and poor graphics. I won’t lie that I feel the exact same till now, but there’s something that makes it different.</p><p>Maybe the reason for not putting much effort in the graphical interface is that the community there is more concerned with the content. In fact, there are many enhancements that are added to the website, but the main dish to the community there is the content-related contribution like problem setting and testing.</p><p>Another reason is that the website holds official programming contests that have a great number of participants, so the speed of the website plays a more important role than the interface. An interface with a lot of animations is a more load on the participant’s connection and processor, where they won’t have time to rate the website style, they only want to submit the first question as fast as possible. Also, maybe they consider this layout to be less distracting.</p><p><em>Then what’s special about Codeforces?</em></p><ol><li>Codeforces is convenient for people who want to improve their problem-solving skills by analyzing a problem statement and coming up with a solution in a way that is simulating a real-life problem.</li><li>There are a lot of problems that help you apply your computer science skills, especially Algorithms and Data Structures. There are many topics that you can classify and go through, and tutorials that help you think in a proper way to apply what you’ve learnt on a problem.</li><li>The most important point about Codeforces is being competitive. Learners who enjoy being in a competitive environment with other learners and want to measure their progress with respect to others will find their souls there. I can say it’s addictive. There are global contests that are held semi-weekly, and competitors try hard to get a good rank. Don’t think that these contests are only for heroes, contests are of different levels so that you can compete with others who aren’t far away from you.</li><li>If you’re planning to take part in a global competitive programming contest <em>(e.g. ICPC, IEEEXtreme)</em>, it’s a great place to train and simulate your participation.</li></ol><h3>Should I learn C++ to start?</h3><p>Mainly, a programming language isn’t an obstacle while implementing an algorithm, because you can implement the algorithm you want in any language with few differences.</p><p>When we get into competitive programming, it’s not only about implementing an algorithm. The problem setters will put constraints to make sure your algorithm doesn’t timeout or consume a lot of memory resources to force you to use an optimal solution. This also affects your choice of programming language, as the execution time of code and usage of memory resources differ from a programming language to another according to the language architecture and details behind the scenes.</p><p>Here are some differences between languages:</p><ul><li><strong>C/C++</strong> provide the least execution time and memory usage in most cases <em>(according to personal experience)</em> but some problems which include long implementation can take a long time to write code and debug because the built-in functions don’t perform all what you expect, so you have to do some manual stuff. They are a great choice if you wish to participate in an official contest, but you should get used to one of them.</li><li><strong>Python</strong> in the other hand takes much longer execution time, but you should be able to solve more than 70% of the problems using it. The time boundaries will bother you in some exact problems where the setter of the problem may have not taken into consideration a reasonable time constraint. The great support of built-in capabilities in <strong>Python</strong> makes the implementation a lot easier, so it helps you think less about common sense operations which would take a long time if you use a lower-level language. I consider it a good choice if you are willing to practice with yourself and don’t care a lot about being an international grandmaster.</li><li><strong>Java</strong> is another good choice for developers who are already used to it. It has great capabilities and a flexible syntax which give the developer control over his code, but it takes longer execution time compared to others.</li></ul><p>So, answering the question, no need to learn C/C++ before digging into competitive programming, but if you wish to master the competitive programming, you may need one of them.</p><h3>Where to start?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xw83-mWD0PmMjACNxLUksA.png" /><figcaption>Codeforces homepage</figcaption></figure><p>When you visit the website, you see something like this: An article, a navbar, some widgets on the right, where should I click to get beginner problems?</p><p>Well, there is no “Beginner Problems” section. Things here are a little different. If you click “Problem Set” from the navbar, it will display all problems from previous contests sorted by their time. Each question has a rating starting from 800 written at the right. Questions rated 800 should be easier than others rated 1000 and so on.</p><p>As a start, you may need to start from a more organized resource. There are private groups that have problems arranged according to their difficulties and topics. Here are some of them from which you can start your journey.</p><ul><li><a href="https://codeforces.com/group/MWSDmqGsZm"><strong>Assiut University Training — Newcomers</strong></a></li><li><a href="https://codeforces.com/group/ZzHCrKVEj7"><strong>ICPC Mansoura Training lvl0 — Newcomers’23</strong></a></li><li><a href="https://codeforces.com/group/isP4JMZTix"><strong>Completed Training From Zero</strong></a></li><li><a href="https://c2-ladders.com/"><strong>C2Ladder</strong></a></li></ul><p>Consider studying the topic referred before getting into problems.</p><p>After a short time, you’ll be able to solve problems from the problem set directly.</p><h3>How to write code?</h3><p>Now let’s get to solve our first problem. I have chosen <a href="https://codeforces.com/problemset/problem/133/A">this problem</a> for the purpose of the tutorial. You can take a look at it before completing the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQNPsK1FDGzDBgG_pZm0dQ.png" /><figcaption>Codeforces Problem Statement</figcaption></figure><p>The statement says that we have a program which behaves according to the given instruction. It can be given many instructions at once, but it ignores unknown ones. The problem will give you the inputted instructions and ask you if the program will give any output.</p><p>We notice that the program will give an output in case the input includes ‘H’, ‘Q’ or ‘9’. It won’t give an output in case of ‘+’ because it will only increment a variable, which has nothing to do with the output. So we know that we want to check if any of the instructions that give an output exists inside the inputted instructions.</p><p>The first step is to read the line of instructions. The input is given through the standard input, so we can use the regular <strong>input()</strong> function. If you’re used to pass a prompt to the <strong>input()</strong> function, don’t do that here; because you’re dealing with a virtual verdict not a real human. If you pass a prompt, it will be considered as a part of your code’s output.</p><p>So, this is what we have now:</p><pre>instructions = input()</pre><p>Now we need to loop through each character in the line to check if one of the instructions we discussed is present. I will use a simple for loop.</p><pre>instructions = input()<br>for instruction in instructions:<br>    if instruction in {&#39;H&#39;, &#39;Q&#39;, &#39;9&#39;}:<br>        print(&quot;YES&quot;)<br>        break</pre><p>So, we check if the instruction is one of the instructions that give an output. Now, if we don’t find a one we need to print a “NO” as described in the statement. For this, python has a feature which allows you to add an else statement to a for loop, where the else statement is executed when the loop completes till end without breaking. So, in our example we know that if the loop continues till end, that means that the instructions we are looking for aren’t present so we should output “NO”.</p><pre>instructions = input()<br>for instruction in instructions:<br>    if instruction in {&#39;H&#39;, &#39;Q&#39;, &#39;9&#39;}:<br>        print(&quot;YES&quot;)<br>        break<br>else:<br>    print(&quot;NO&quot;)</pre><p>That’s it! You’ve written your first solution to a Codeforces problem.</p><p>There’s also another solution which converts the string input into a set and checks if the two sets intersect. If you aren’t familiar with sets never mind.</p><pre>instructions = input()<br>if set(instructions).intersection({&#39;H&#39;, &#39;Q&#39;, &#39;9&#39;}):<br>    print(&quot;YES&quot;)<br>else:<br>    print(&quot;NO&quot;)</pre><p>Now time to submit your solution. You can submit by either uploading the code file or copying the code directly into the website.</p><p>You will see this widget in the right sidebar:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*RSTMURjuFESeU561AnQIpA.png" /><figcaption>Submit file</figcaption></figure><p>To copy &amp; paste directly, click this in the navbar:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*h7ld63AQKcqjOY5eEUdRLQ.png" /><figcaption>Problem Navbar</figcaption></figure><p>In the language field, you can choose either <strong>Python 3</strong> or <strong>PyPy 3.9</strong>. In most cases, <strong>PyPy</strong> will be faster, and you’ll face less problems with it.</p><p><a href="https://en.wikipedia.org/wiki/PyPy"><strong>More about PyPy</strong></a></p><h3>How do I deal with complex inputs?</h3><p>Not all inputs are strings, sometimes you are given a number, or many numbers on one line, or a 2D array on multiple lines. How to handle that? Let’s see.</p><p>Let’s assume the input is two words, where each word is in a separate line. In that case we will use the same <strong>input()</strong> method, but we will call it twice. We can call it as long as we have more lines to read.</p><pre>&quot;&quot;&quot;<br>programming<br>article<br>&quot;&quot;&quot;<br><br>word1 = input()<br>word2 = input()</pre><p>Let’s get into another example, where the words are on a single line, separated by a space. For that we use a string method called <strong>split()</strong> which splits the string into an array of smaller strings and uses the space as a delimiter. If you need a special delimiter, you can pass it to the function.</p><pre>&quot;&quot;&quot;<br>programming article<br>&quot;&quot;&quot;<br><br>word1, word2 = input().split()</pre><p>In case you don’t know the count of words or they’re just too much to handle with single variables, you can store them into a list and access them using indices.</p><pre>&quot;&quot;&quot;<br>programming article about codeforces<br>&quot;&quot;&quot;<br><br>words = input().split()<br><br># words[0] -&gt; programming<br># words[1] -&gt; article<br># words[2] -&gt; about</pre><p>What if we have a number? that’s fine, we have the functions <strong>int()</strong>, <strong>float()</strong> which convert the string value into a numerical value which you can deal with in means of mathematics.</p><pre>&quot;&quot;&quot;<br>3<br>5.68<br>&quot;&quot;&quot;<br><br>x = int(input())<br>y = float(input())</pre><p>But what if we have many numbers on a single line? we want to use <strong>split()</strong>, but it’s a string function which only return a list of string values. So, we need a way to convert each value of the list into a number. For this we have a special function which is <strong>map()</strong>. It takes two arguments, the function you would like to apply to all elements and the collection of elements (list). It returns a map object which can be assigned directly to variables or casted into a new list.</p><pre>&quot;&quot;&quot;<br>2 3 5<br>11 99 44 22 66<br>&quot;&quot;&quot;<br><br>a, b, c = map(int, input().split())<br>numbers = list(map(int, input().split()))<br></pre><p>Now let’s consider a whole example where you are given the dimensions of a 2-dimensional array, and then you are given the array elements on multiple lines. How to deal with it? there’s nothing new, we will just use the same methods, and use a loop for taking all the rows of the 2D array. Let’s see:</p><pre>&quot;&quot;&quot;<br>4 3<br>1 2 3<br>9 3 4<br>8 7 2<br>1 0 5<br>&quot;&quot;&quot;<br><br>rows, columns = map(int, input().split())<br>arr = [ list(map(int, input().split())) for i in range(rows) ]</pre><p>I used a shorthand for loop, but you are able to use the regular one and append each row to the 2d array.</p><p>In some problems, the first line of input contains the number of test cases, then each test case has a specific input. Let’s talk about <a href="https://codeforces.com/contest/1829/problem/A">this problem</a> for example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iSvPfbeJCgZj946LADJhqQ.png" /><figcaption>Input and Output specifications</figcaption></figure><p>The issue here is that we want to execute the solution we have written a number of times, which is equal to the number of test cases. So, one approach is wrapping the whole code inside a for loop. See the solution below:</p><pre>for t in range(int(input())):<br>    word1 = input()<br>    word2 = &#39;codeforces&#39;<br> <br>    differences = 0<br>    for i in range(10):<br>        if word1[i] != word2[i]:<br>            differences += 1<br>    print(differences)</pre><p>Note that if we remove the first line and the indentation, The code will work perfectly for one test case. So, the advantage of this way is that I don&#39;t have to change anything except wrapping the whole code inside a for loop.</p><h3>How to input/output from file?</h3><p>In some problems, you will see the input and the output are files. In this case, you can’t use normal stdin, stdout methods. There are special methods in Python that deal with files, and you can use them as desired.</p><p>There are many ways to achieve this, but I will mention ways that I see more straightforward and clearer.</p><p>There’s a built-in python module for reading input from files, which is <strong>fileinput</strong>. You can use it in the following way:</p><pre>&quot;&quot;&quot;<br>hello<br>&quot;&quot;&quot;<br><br>import fileinput<br>fin = fileinput.input(&#39;scoreboard.in&#39;)<br><br>text = fin.readline()</pre><p>You may not enjoy writing a lot of <strong>fin.readline()</strong> in your code in case you are used to the regular <strong>input()</strong> method. This is solvable as you can override the <strong>input()</strong> method to work as you expect:</p><pre>&quot;&quot;&quot;<br>hello<br>&quot;&quot;&quot;<br><br>import fileinput<br>fin = fileinput.input(&#39;scoreboard.in&#39;)<br>input = fin.readline<br><br>text = input()</pre><p>Regarding the output, you can direct the output to a file by changing the value of <strong>sys.stdout</strong> to match the output file.</p><pre>import sys<br>fout = open(&#39;scoreboard.out&#39;, &#39;w&#39;)<br>sys.stdout = fout<br><br>print(&quot;Hello, file&quot;)</pre><h3>How can I increase my rating?</h3><p>After you’ve been solving problems for a while, you think you are now ready to fight. If you want to participate in the official website contests, just click on “Contests” from the navbar. It will show you the upcoming and previous contests. To take part in a contest, you should register for it before registration is closed. A contest has a definite time which means you have to be ready in the specified time to be considered an official participant. Later, you can solve the problems at any time, but this won’t affect your official participations. After the contest is over, your rating changes according to your previous rating and your contest ranking. The rating may take time to appear on your profile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xo1hPg548WxZP1XEPe3W5w.png" /><figcaption>Codeforces Contests Page</figcaption></figure><p>Contestants are divided into ranks based on their ratings. Since May 2018, users with ratings between 1900 and 2099 can be rated in both Div. 1 and Div. 2 contests. At the same time, Div. 3 was created for users rated below 1600. There is also Div. 4, which is for users rated below 1400.</p><p>I think there is no need to dig deeper, because you will for sure find your way and be a part of the community if you enjoy the weather there. This article targets people who are puzzled and don’t know where or how to start, but once you get into the way, you can do a lot.</p><p>Also note that I’m not a professional competitive programmer, but I wanted to help you in your first steps because I have been in your situation earlier. Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c5ea6ad9f04" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>