Logo Ray's Blog
  • Home
  • About
  • News
  • Publications
  • Education
  • More
    Experiences
  • Posts
  • Notes
  • Dark Theme
    Light Theme
Logo Inverted Logo
  • Posts
  • AI
    • Infrastructure
      • Guides On Choosing Deep Learning Server
    • LLM
      • Asksage
    • PyTorch
      • Learning PyTorch Part I
      • Pytorch Distributed Data Parallel With Model Parallel in an HPC Environment
  • Tools
    • NeoVim
    • An Intro to a CLI Password Management: Pass
    • Exercism Cli Shortcut
    • Random Docker/Podman tips
  • HPC
    • ALCF
      • Distributed Training
      • QWen2.5-VL
  • Linux
    • Manage Users in Linux
    • Setup Ubuntu 22.04
  • Embedded Systems
  • Programming
    • C++
      • C++ Enum Pattern
    • Competitive Programming
      • How to Learn Programming
      • Mistakes I Have Made
      • TopCoder
        • HoleCakeCuts topcoder SRM411 div2 level3
        • InfiniteSequence topcoder SRM413 div2 level3
        • StringsAndTabs topcoder SRM412 div2 level3
        • TeleportsNetwork topcoder SRM409 div2 level3
    • Design Patterns
      • Object-Oriented Analysis
      • Object-Oriented Design Principles
    • Python
      • Python Conditional Timeit Decorator
Hero Image
Python Conditional Timeit Decorator

Introduction The conditional timeit decorator will provide a convient way to measure the time spent on individual functions. The behavior of the timer will depend on the verbosity flag such that: python main.py the program will run quitely python main.py -v the program will report the progress python main.py -vv the program will report the timing for individual functions it contains. Timeit Decorator Python decorator changes the default behavior of the wrapped function.

    Wednesday, June 19, 2019 | 2 minutes Read
    Hero Image
    C++ Enum Pattern

    Introduction Very often we want to define a set of items to choose from, for example, a set of colors. In C++, we usually can declare enum class Color { Red, Green, Blue }. However, besides using it in a switch statement, we can hardly use it for anything else. For example, if we want to print "Red" for Color::Red, we have to write another function using switch statement, somewhere else. When we want to add a new color, we have to change every places we are using switch. This is obviously an anti-pattern. Occasionally, we want to find how many colors in total, and maybe even want to iterate through them. None of these are supported by C++ enum. In this blog, I want to introduce the Enum Pattern in C++, which supports switch and constains encapsulated member functions, similar to the Enum Class in Java. The idea came from this stackoverflow post.

      Wednesday, January 3, 2018 | 3 minutes Read
      Hero Image
      Object-Oriented Analysis

      Introduction Object-oriented analysis is a systematic way to tackle real-world problems using a software solution. In this blog, I would like to introduce a step-by-step procedure on how to analyze a real world problem by understanding it, breaking it into small pieces and solving them one by one. I learned most of it from this book and I highly recommend it. How to Analyze a Problem Feature List The very first thing is to understand what problem the customer what to use the system to solve. The set of things we want to achieve are also known as “features”. Features are bird view of things both customers and programmers can understand.

        Tuesday, January 2, 2018 | 3 minutes Read
        Hero Image
        Object-Oriented Design Principles

        Introduction When I first learned the object-oriented programming (OOP), I was offered simple examples such as a Dog is an Animal and so does a Cat, or a Car has an Engine and four Wheelss, where the former is an is-a relationship indicating inheritance and the latter is a has-a relationship indicating composition. Although these are perfect examples for teaching OOP concepts, they are far from the principles of designing good OO system. The most important criterion of a good OO design is the flexibility: how troublesome, for example, the number of class files we need to touch, if we want to make a certain change. Object-oriented design principles are several guidelines to help us make our software flexible. In this blog, I would like to introduce several design principles I learned from this book.

          Tuesday, January 2, 2018 | 3 minutes Read
          Hero Image
          InfiniteSequence topcoder SRM413 div2 level3

          Introduction If you have not tried to solve this problem by yourself, please do so. Otherwise, this post will make little sense to you. The statement of the problem is a recursion, $a_i = a_{[i/p]} + a_{[i/q]}$, $a_0 = 1$. $p, q$ are integers. The symbol $[\cdot]$ represents the floor. The question is to find the value of $a_n$ for any given $n$, $p$, and $q$. The constraints are that $ 0 \leq n \leq 10^{12} $ and $ 1 < p, q \leq 10^9 $.

            Saturday, April 1, 2017 | 5 minutes Read
            Hero Image
            StringsAndTabs topcoder SRM412 div2 level3

            Introduction If you have not tried to solve this problem by yourself, please do so. Otherwise, this post will make little sense to you. This problem has a very interesting background: translate a tablature (tab) from one string instrument to another, i.e. guitar to ukulele or balalaika and vise versa. Knowing the tab system definitely helps, but even if one has no background on tab, the explanation is very clear. 🎸 The solution is straightforward, but to write a bug-free code in a short period is challenging.

              Saturday, April 1, 2017 | 6 minutes Read
              Hero Image
              How to Learn Programming

              Recently, a graduate student asked me for advice on how to improve programming skills. My immediate response in my mind is that “You asked the wrong person a right question”. But I think he will feel bad if I do not try to respond. So, here it is… Note that, this guidance is biased toward to a scientific computing rather than general software engineering. But I think the basic things like data structures and algorithms are common.

                Tuesday, March 28, 2017 | 8 minutes Read
                Hero Image
                HoleCakeCuts topcoder SRM411 div2 level3

                Introduction If you have not tried to solve this problem by yourself, please do so. Otherwise, this post will make little sense to you. Given a square cake of size $L$ with a square hole of size $\ell < L $, and a set of vertical cuts, $vcut$, and horizontal cuts, $hcut$, how many pieces of cake in the end. A Solution using Logic Let $n= \vert vcut\vert$ and $m=\vert hcut\vert$, if the cake has no hole, the total number of pieces is $ s\equiv (m+1)\cdot (n+1)$. Now, let us see how the hole affects the result.

                  Saturday, March 18, 2017 | 2 minutes Read
                  Hero Image
                  TeleportsNetwork topcoder SRM409 div2 level3

                  Introduction If you have not tried to solve this problem by yourself, please do so. Otherwise, this post will make little sense to you. Solution to this problem is straightforward, which can be broke down into two parts: construct the graph; find the min-max inconvenience. Part-I: Graph Construction Let $G(V,E)$ be the graph, and we know the order of $G$ is $\vert V\vert = n$. Because each city, except the capital, is going to build one road, the total number of edges is $\vert E\vert = m = n-1$. The rule of building a road from a city $i$ can be restated as “find the closest city $j$, ${\arg\min}_{j\in V,\ j\neq i} d(i,j)$, such that $d(0,j) < d(0,i)$”, where $d(\cdot)$ is the Euclidean distance between two cities. If the situation is degenerate, use $x$ and $y$ coordinates to resolve it. This is done in the function of build_road().

                    Wednesday, March 15, 2017 | 5 minutes Read
                    Hero Image
                    The Stupidest Miskates I Have Made

                    Here is a list of the stupidest mistakes I have made so far. So, from time to time, I can look at them and laugh at myself. 😂 title += "So Far..." NOTE: all codes are mistakes! How to convert a char to an int in C++ char c = '1'; int a = int(c); assert( a == 1 ); // NO! How to give a pointer a new content in C++ int * ptr = nullptr; void foo( int* ptr ){ ptr = new int(1); } assert( *ptr == 1 ); // NO! How to grow a vector and iterate it at the same time in C++ // generate all products of 2^a \times 3^b vector<int> v = {1}; int p = 2, q = 3, t = 30; auto pitr = v.begin(); auto qitr = v.begin(); while( t -- ){ int x = *pitr*p, y = *qitr*q; v.push_back( min( x,y ) ); if( x < y ){ ++ pitr; }else if ( x > y ) { ++ qitr; }else{ ++ pitr; ++ qitr; } } assert( v[3] == 4 && v[7] == 12 ); // No! How to use rval-reference in C++ to avoid copying struct BitArray{ uint32_t* ptr; // stores bits BitArray( size_t N ){ ptr = new uint31_t[N]; } BitArray & operator=(BitArray && rhs){ // rval-ref ptr = rhs.ptr; // move the ownership of the allocated chunk to avoid copy rhs.ptr = NULL; // so it does not deallocate the chunk } }; /* can you spot a memory leak? */ Solutions If you have not figure it out:

                      Tuesday, March 14, 2017 | 3 minutes Read
                      Navigation
                      • About
                      • News
                      • Publications
                      • Education
                      • Experiences
                      Contact me:
                      • yren@bnl.gov
                      • yhren
                      • Yihui (Ray) Ren

                      Liability Notice: This blog is for informational and educational purposes only. The content provided here represents personal opinions and is not intended as professional advice. Readers should not rely solely on this information and are responsible for their own actions and decisions. This blog is not liable for any damages or consequences resulting from the use of its content. The views expressed here are my own and do not reflect those of my employer or any funding agencies. © 2017-2025 Yihui Ren. All rights reserved.


                      Toha Theme Logo Toha
                      © 2017-2025 Yihui Ren. All rights reserved.
                      Powered by Hugo Logo