I have been wanting to write this article for a long time. It all started when I encountered the problem LeetCode#387 . The English version of the article was completed in August 2022, and the Chinese translation came almost a year later – in July 2023.
Solution
The simplest way to achieve this task is by utilizing ASCII code.
Regarding ASCII code, let me briefly introduce it. It is essentially a coding scheme that originated from telegraph development. The main displayable range includes English letters, numbers, and some special characters.
The initial development of this encoding was for universal use, but it was primarily based on the Latin alphabet. Later, to address its limitations, Unicode was introduced, also known as the Universal Character Set (ASCII code is included in it).
Unicode assigns a unique code to each character, rather than defining a unique code for each glyph (i.e., an integer for each character).
The differences between the two lie not only in the range but also in the encoding method.
ASCII code uses 7 bits, meaning it can store 2 to the power of 7, which is 128 characters.
As for Unicode, it utilizes hexadecimal notation for storage.
For more detailed information, you can refer to the wiki, which lists the decimal numbers that each character will display.
After going through some details, let’s get back to the main point.
Since various programming languages process text using Unicode, and we know it is stored in hexadecimal, if you haven’t taken shortcuts in learning computer fundamentals, you should be aware that hexadecimal can be easily converted to decimal.
For example, let’s take the Chinese character “我” (pronounced as “wǒ“). Here, I’ll use C# as an example.
The Unicode code point for the character “我” is U+6211, which is equivalent to the decimal value 25105.
char a = '我';
// Print the Unicode code point of the character '我'
Console.Write((int)a);
// Print the character corresponding to the Unicode code point 25105
Console.WriteLine((char)25105);
This example can also validate this point.
And at this point, whether it’s Unicode or ASCII code, you should observe that the decimal values for A to Z are consecutive numbers.
Therefore, combining the above numerical characteristics, you can use a simple loop to generate the results~
C#
for (char c = 'a'; c <= 'z'; c++)
{
Console.WriteLine(c);
}
That’s right; you can print out all the lowercase letters this way.
It’s important to note that the lowercase and uppercase letters are not consecutive.
The decimal values for uppercase A to Z range from 65 to 90.
The decimal values for lowercase a to z range from 97 to 122.
(This is a composition I cut and assembled from the information on the wiki. Apologies if it looks messy.)
So, if you want to print from lowercase ‘a’ to uppercase ‘Z’
for (char c = 'a'; c <= 'z'; c++)
{
Console.WriteLine(c);
}
for (char c = 'A'; c <= 'Z'; c++)
{
Console.WriteLine(c);
}
In the context of the following languages, we will specifically use lowercase ‘a’ to ‘z’ as an example (for any uncertainties, please refer to the explanation in C#).
Java
for (char c = 'a'; c <= 'z'; c++)
{
//System.out.println((int)c);
System.out.println(c);
}
Python3
for c in range(ord('a'), ord('z') + 1):
# Print the Unicode decimal value
# print(c)
# Print the character using the Unicode decimal value
print(chr(c))
In Python, it’s a bit different; you can’t directly loop over characters. In this case, you need to use the built-in function ord, which returns the Unicode decimal code for a given character.
Then, you can use range to iterate over the decimal codes.
Additionally, you can convert the decimal code back to the corresponding character using the built-in function chr.
(Alternatively, if you prefer to directly represent characters using numbers, that’s also possible. However, generally, who remembers those, haha!)
for c in range(97, 122 + 1):
#print(c) # Print the Unicode decimal value
print(chr(c))
JavaScript
for (let c = 'A'.charCodeAt(0); c <= 'Z'.charCodeAt(0); c++) {
//console.log(c); //# Print the Unicode decimal value
console.log(String.fromCharCode(c))
}
(Similar to Python, you still need to use numbers to drive the loop.)
Conclusion
This time, I introduced four languages that I commonly use.
If there’s a need for additional languages or any areas that require further clarification, please feel free to leave a comment to let me know!
🧡You can support me by sharing my posts or clicking some ads, Thanks a lot
✅If you got any problem about this post, please feel free to ask me ~
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"c29a2a3554"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"c29a2a3554","uagb_grid_ajax_nonce":"46543fc588"};
( function() {
let elements = document.querySelectorAll( '.uagb-post-grid.uagb-block-efe84f35 .uagb-post-pagination-wrap a' );
elements.forEach(function(element) {
element.addEventListener("click", function(event){
event.preventDefault();
const link = event.target.getAttribute('href').match( /\/page\/\d+\// )?.[0] || '';
const regex = /\d+/; // regular expression to match a number at the end of the string
const match = link.match( regex ) ? link.match( regex )[0] : 1; // match the regular expression with the link
const pageNumber = parseInt( match ); // extract the number and parse it to an integer
window.UAGBPostGrid._callAjax({"btnBorderStyle":"none","block_id":"efe84f35","categories":"215","btnBorderLink":true,"btnBorderRadiusLink":true,"overallBorderLink":true,"overallBorderRadiusLink":true,"inheritFromTheme":true,"postType":"post","postDisplaytext":"No post found!","taxonomyType":"category","postsToShow":6,"enableOffset":false,"postsOffset":0,"displayPostDate":true,"displayPostExcerpt":true,"excerptLength":15,"displayPostAuthor":false,"displayPostTitle":true,"displayPostComment":true,"displayPostTaxonomy":false,"hideTaxonomyIcon":true,"taxStyle":"default","displayPostTaxonomyAboveTitle":"withMeta","displayPostImage":true,"imgSize":"large","imgPosition":"top","bgOverlayColor":"#000000","overlayOpacity":"50","displayPostLink":true,"newTab":false,"ctaText":"Read More","btnHPadding":"","btnVPadding":"","columns":3,"tcolumns":2,"mcolumns":1,"align":"left","width":"wide","order":"desc","orderBy":"date","rowGap":20,"rowGapTablet":20,"rowGapMobile":20,"columnGap":20,"bgType":"color","bgColor":"#f6f6f6","titleTag":"h4","titleFontSize":"","titleFontSizeType":"px","titleFontFamily":"","titleLineHeightType":"em","titleLoadGoogleFonts":false,"metaColor":"","highlightedTextColor":"#fff","highlightedTextBgColor":"#3182ce","metaFontSize":"","metaFontSizeType":"px","metaFontFamily":"","metaLineHeightType":"em","metaLoadGoogleFonts":false,"excerptColor":"","excerptFontSize":"","excerptFontSizeType":"px","excerptFontFamily":"","excerptLineHeightType":"em","excerptLoadGoogleFonts":false,"displayPostContentRadio":"excerpt","ctaBgType":"color","ctaBgHType":"color","ctaFontSize":"","ctaFontSizeType":"px","ctaFontFamily":"","ctaLineHeightType":"em","ctaLoadGoogleFonts":false,"paddingTop":20,"paddingBottom":20,"paddingRight":20,"paddingLeft":20,"contentPadding":20,"ctaBottomSpace":0,"ctaBottomSpaceTablet":0,"ctaBottomSpaceMobile":0,"imageBottomSpace":15,"titleBottomSpace":15,"metaBottomSpace":15,"excerptBottomSpace":25,"excludeCurrentPost":false,"contentPaddingUnit":"px","rowGapUnit":"px","columnGapUnit":"px","excerptBottomSpaceUnit":"px","paginationSpacingUnit":"px","imageBottomSpaceUnit":"px","titleBottomSpaceUnit":"px","metaBottomSpaceUnit":"px","ctaBottomSpaceUnit":"px","paddingBtnUnit":"px","mobilePaddingBtnUnit":"px","tabletPaddingBtnUnit":"px","paddingUnit":"px","mobilePaddingUnit":"px","tabletPaddingUnit":"px","isPreview":false,"taxDivider":", ","titleLetterSpacing":"","titleLetterSpacingType":"px","metaLetterSpacing":"","metaLetterSpacingType":"px","ctaLetterSpacing":"","ctaLetterSpacingType":"px","excerptLetterSpacing":"","excerptLetterSpacingType":"px","useSeparateBoxShadows":true,"boxShadowColor":"#00000070","boxShadowHOffset":0,"boxShadowVOffset":0,"boxShadowBlur":"","boxShadowSpread":"","boxShadowPosition":"outset","boxShadowColorHover":"","boxShadowHOffsetHover":0,"boxShadowVOffsetHover":0,"boxShadowBlurHover":"","boxShadowSpreadHover":"","boxShadowPositionHover":"outset","borderWidth":"","borderStyle":"none","borderColor":"","borderRadius":"","blockName":"post-grid","equalHeight":true,"postPagination":false,"pageLimit":10,"paginationBgActiveColor":"#e4e4e4","paginationActiveColor":"#333333","paginationBgColor":"#e4e4e4","paginationColor":"#777777","paginationMarkup":"","paginationLayout":"filled","paginationBorderColor":"#888686","paginationBorderSize":1,"paginationSpacing":20,"paginationAlignment":"left","paginationPrevText":"\u00ab Previous","paginationNextText":"Next \u00bb","layoutConfig":[["uagb\/post-image"],["uagb\/post-taxonomy"],["uagb\/post-title"],["uagb\/post-meta"],["uagb\/post-excerpt"],["uagb\/post-button"]],"post_type":"grid","equalHeightInlineButtons":false,"imageRatio":"inherit","imgEqualHeight":false,"paginationType":"ajax","extended_widget_opts_block":{},"extended_widget_opts":{},"extended_widget_opts_state":"","extended_widget_opts_clientid":"","dateUpdated":""}, pageNumber, 'efe84f35');
});
});
} )();
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};