Giter Site home page Giter Site logo

scorp's Introduction

func get_posts(user_id, post_ids): if not post_ids: return []

    db_posts = f'SELECT id, description, image, created_at FROM post WHERE user_id={user_id} AND id IN {post_ids}'
    db_user = f'SELECT id, username, full_name, profile_picture FROM user WHERE id={user_id}'
    db_user.followed = f'SELECT following_id FROM follow WHERE EXISTS (SELECT following_id FROM follow where following_id={user_id})'
    posts_map = {}

    for post in db_posts:
            setattr(post, 'owner', db_user )
            posts_map[post.id] = post
    
    if posts_map:
            for liked in f'SELECT * FROM like WHERE post_id in {posts_map.keys()}':
                    posts_map[liked.post_id].update({'liked': liked.user_id == user_id})
            
    return [posts_map.get(post_id, None) for post_id in post_ids] if posts_map else []

def merge_two_sorted_posts(first_posts_list, second_posts_list, seen): if len(first_posts_list) == 0: return second_posts_list[:] elif len(second_posts_list) == 0: return first_posts_list[:] else: if first_posts_list[0].get('created_at') == second_posts_list[0].get('created_at'): if first_posts_list[0].get('id') > second_posts_list[0].get('id'): return [first_posts_list[0]] + merge_two_sorted_posts(first_posts_list[1:], second_posts_list, seen) else: return [second_posts_list[0]] + merge_two_sorted_posts(first_posts_list, second_posts_list[1:], seen)

            elif first_posts_list[0].get('created_at') > second_posts_list[0].get('created_at'):
                    return [first_posts_list[0]] + merge_two_sorted_posts(first_posts_list[1:], second_posts_list, seen)
            else:
                    return [second_posts_list[0]] + merge_two_sorted_posts(first_posts_list, second_posts_list[1:], seen)

def merge_posts(list_of_posts): list_posts = [] seen = set() for posts in list_of_posts: list_posts = merge_two_sorted_posts(list_posts, posts, seen) seen.update([post['id'] for post in posts]) return list_posts

scorp's People

Contributors

elayira avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.